ecccec7950
Moved AccountMeta provider into <User> context and migrated to using a FutureProvider to perform the network request NOTE: Bug was caused by AccountMeta? being loaded late, causing the root provider to be reloaded.
74 lines
1.8 KiB
Dart
74 lines
1.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:outbag_app/backend/errors.dart';
|
|
import 'package:outbag_app/backend/request.dart';
|
|
import 'package:outbag_app/tools/snackbar.dart';
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
Future<void> doNetworkRequest(ScaffoldMessengerState? sm,
|
|
{required Future<Response> Function() req,
|
|
Function(Map<String, dynamic> body)? onOK,
|
|
bool Function()? onNetworkErr,
|
|
bool Function()? onUnknownErr,
|
|
Function()? onAnyErr,
|
|
Function()? after,
|
|
bool Function(Map<String, dynamic>)? onServerErr}) async {
|
|
AppLocalizations? trans = (sm!=null)?AppLocalizations.of(sm.context):null;
|
|
|
|
Response res;
|
|
try {
|
|
res = await req();
|
|
} catch (_) {
|
|
// network error
|
|
bool showBar = true;
|
|
|
|
if (onNetworkErr != null) {
|
|
showBar = onNetworkErr();
|
|
}
|
|
|
|
if (showBar && sm != null) {
|
|
showSimpleSnackbar(sm, text: trans!.errorNetwork, action: trans.dismiss);
|
|
}
|
|
if (onAnyErr != null) {
|
|
onAnyErr();
|
|
}
|
|
if (after != null) {
|
|
after();
|
|
}
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (res.res == Result.ok) {
|
|
if (onOK != null) {
|
|
await onOK(res.body);
|
|
}
|
|
} else {
|
|
// server error
|
|
bool showBar = true;
|
|
|
|
if (onServerErr != null) {
|
|
showBar = onServerErr(res.body);
|
|
}
|
|
if (showBar && sm != null) {
|
|
showSimpleSnackbar(sm, text: errorAsString(res.body, trans!), action: trans.ok);
|
|
}
|
|
if (onAnyErr != null) {
|
|
onAnyErr();
|
|
}
|
|
}
|
|
} catch (_) {
|
|
bool showBar = true;
|
|
|
|
if (onUnknownErr != null) {
|
|
showBar = onUnknownErr();
|
|
}
|
|
|
|
if (showBar && sm != null) {
|
|
showSimpleSnackbar(sm, text: trans!.errorUnknown, action: trans.ok);
|
|
}
|
|
}
|
|
|
|
if (after != null) {
|
|
after();
|
|
}
|
|
}
|