dio 최신 버전 추가
class HttpRequest {
//https://pub.dev/packages/dio
static final _dio = Dio();
static Future<HttpResponseResult?> fetchPost<E>(String url, HttpHeaderModel header, E parameter) async {
String requestBodyJson = jsonEncode(parameter);
Map<String, String> headerMap = _getSendHeaderMap(header);
try {
var response = await _dio.request(
HttpConst.baseUrlLocal + url,
data: requestBodyJson,
options: Options(method:'POST', headers: headerMap, followRedirects: false,
validateStatus: (status) {
return true; //status! < 500;
})
);
//int? status = response.statusCode;
return HttpResponseResult.fromJson(response.data);
} on DioError catch (e) {
print(e);
} catch(ex) {
print(ex);
}
return null;
}
static Map<String, String> _getSendHeaderMap(HttpHeaderModel header) {
Map<String, String> headerMap = HashMap();
if(UserInformation.accessToken.isNotEmpty) {
headerMap[HttpConst.accessToken] = UserInformation.accessToken;
}
headerMap[HttpConst.requestTime] = "${header.requestTime}"; //header.requestTime;
headerMap[HttpConst.transactionNumber] = header.transactionNumber;
headerMap["Content-type"] = "application/json";
headerMap["Accept"] = "application/json";
return headerMap;
}
}
보낼 때 사용 되는 Requester
class HttpHeaderModel {
late String requestTime;
late String transactionNumber;
late String responseTime;
late String response;
}
Header Class 암호화를 사용하지 않는다면 fetchPost에서 Header는 제외하고 사용해도 된다.
void login(String email, String password) {
HttpHeaderModel httpHeaderModel = HttpHeaderModel();
httpHeaderModel.transactionNumber = "1000";
final now = DateTime.now();
final result = HttpRequest.fetchPost<LoginModel>(http_url.login, httpHeaderModel
, LoginModel(email: email, password: SecurityUtil.getSecureAes256Encrypt(password, 암호값)));
result.then((value) => {
if(value == null || !value.status) {
CustomDialog.confirm(context, title: '로그인 실패', content: '로그인에 실패하였습니다. 이메일/비밀번호 확인', positive: DialogAction('확인', () => true))
} else {
if(value.status) {
UserInformation.isLoginSuccess = true,
UserInformation.accessToken = "${value.data["accessToken"]}",
print(UserInformation.accessToken),
/*Navigator.push(context, MaterialPageRoute(builder: (context) => const RegisterPage(title: 'asdf',)),),*/
/*Navigator.push(context, MaterialPageRoute(builder: (context) => const MainPage())),
Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false),*/
Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (BuildContext context) => const MainPage()), (route) => false)
}
}
});
}
onPressed: () {
if (_formKey.currentState!.validate()) {
loginService.login(emailTextEditController.text, passwordTextEditController.text);
}
},
'Flutter' 카테고리의 다른 글
06. Flutter Future build 이벤트 방지 (0) | 2022.09.19 |
---|---|
05. Flutter build 종료 후 이벤트 (0) | 2022.09.16 |
04. Flutter 상태 저장 shared_preferences (0) | 2022.09.16 |
03. Fultter Datatable 사용 (0) | 2022.09.07 |
01. 플러터 설치 (0) | 2021.07.26 |