postRequest<L extends Link> method
Future<RequestResult<L?> >
postRequest<L extends Link>(
- LinkNotifier<
L> ? notifier, { - GlobalKey<
ScaffoldState> ? globalKey, - dynamic errorCallback()?,
- dynamic readyLock = true,
- int timeout = 7,
Implementation
Future<RequestResult<L?>> postRequest<L extends Link>(
LinkNotifier<L>? notifier,
{GlobalKey<ScaffoldState>? globalKey,
Function()? errorCallback,
readyLock = true,
int timeout = 7}) async {
if (readyLock) {
if (notifier!.state != LinkState.ready)
return RequestResult(false, notifier.link);
notifier.state = LinkState.progress;
}
http.Response response;
try {
response = await http
.post(Uri.parse(serverUrl),
body: jsonEncode(notifier!.link!.getSendJSON()))
.timeout(Duration(seconds: timeout));
} on Exception catch (e) {
Debug.debugging(
"HttpPost", "http request timeout $e, by ${notifier!.link}");
_showNetErrSnackBar(globalKey);
if (errorCallback != null) errorCallback();
notifier.state = LinkState.ready;
return RequestResult(false, notifier.link);
}
if (response.statusCode == 200) {
try {
var json = jsonDecode(response.body);
notifier.receive(json);
return RequestResult(true, notifier.link,
result: json[BKConstants.BK_WORKING_RESULT]);
} catch (e, stack) {
String errorMessage =
"HttpRequest receive error, error message:$e, response body:${response.body}"
" link${notifier.link}";
Debug.debugging("HttpRequest", errorMessage);
Debug.debugging("HttpRequest", stack.toString());
_showNetErrSnackBar(globalKey);
}
} else {
_showNetErrSnackBar(globalKey);
notifier.state = LinkState.ready;
Debug.debugging("HttpPost", "network error");
}
return RequestResult(false, notifier.link);
}