send<T> method
Awaits a service call's (ok, error, exception) result: on success toasts and runs reload, otherwise toasts the error.
Returns the ok response (on create its result is the new id), or null when it failed.
Implementation
Future<T?> send<T>(Future<(T?, Object?, String?)> call, VoidCallback? reload) async {
final (T? ok, Object? error, String? exception) = await call;
if (ok == null) {
final String? errorMessage = switch (error) {
final UEmptyResponse e => e.message,
final UResponse<dynamic> e => e.message,
_ => null,
};
UToast.error(message: errorMessage.nullIfEmpty() ?? exception.nullIfEmpty() ?? U.s.errorSubmittingForm);
return null;
}
final String? okMessage = switch (ok) {
final UEmptyResponse r => r.message,
final UResponse<dynamic> r => r.message,
_ => null,
};
UToast.snackBar(message: okMessage.nullIfEmpty() ?? U.s.submitted);
reload?.call();
return ok;
}