9ff6d97c90
BUG: when deleting a product the product list won't update
76 lines
1.8 KiB
Dart
76 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) {
|
|
await after();
|
|
}
|
|
}
|