Fix crash when a requests fails in non-widget mode (aka on background-receive): Cannot show toasts without an UI

This commit is contained in:
2025-05-23 21:10:20 +02:00
parent cc13f8a0f3
commit faa624e9f8
5 changed files with 56 additions and 14 deletions

View File

@@ -205,7 +205,7 @@ void main() async {
final appLaunchNotification = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
if (appLaunchNotification != null) {
// Use has launched SCN by clicking on a loca notifiaction, if it was a summary or message notifiaction open the corresponding screen
// Use has launched SCN by clicking on a local notifiaction, if it was a summary or message notifiaction open the corresponding screen
// This is android only
//TODO same on iOS, somehow??
ApplicationLog.info('App launched by notification: ${appLaunchNotification.notificationResponse?.id}');
@@ -216,6 +216,8 @@ void main() async {
ApplicationLog.debug('[INIT] Application started');
Globals().appWidgetInitialized = true;
runApp(
MultiProvider(
providers: [

View File

@@ -17,6 +17,8 @@ class Globals {
bool _initialized = false;
bool appWidgetInitialized = false;
String appName = '';
String packageName = '';
String version = '';

View File

@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:simplecloudnotifier/state/globals.dart';
import 'package:toastification/toastification.dart';
class Toaster {
@@ -10,6 +11,13 @@ class Toaster {
static final borderRadius = BorderRadius.circular(4.0);
static void simple(String title) {
print('[TOAST]: [[SIMPLE]] $title');
if (!Globals().appWidgetInitialized) {
print('[TOAST]: Cannot show simple-toast - app<widget> not initialized - skipping');
return;
}
toastification.show(
type: ToastificationType.success,
style: ToastificationStyle.simple,
@@ -17,7 +25,7 @@ class Toaster {
description: Text(title),
autoCloseDuration: autoCloseDuration,
borderRadius: borderRadius,
closeButtonShowType: CloseButtonShowType.none,
closeButton: ToastCloseButton(showType: CloseButtonShowType.none),
alignment: alignment,
animationDuration: animationDuration,
pauseOnHover: false,
@@ -28,6 +36,13 @@ class Toaster {
}
static void success(String title, String message) {
print('[TOAST]: [[SUCC]] $title:\n$message');
if (!Globals().appWidgetInitialized) {
print('[TOAST]: Cannot show succ-toast - app<widget> not initialized - skipping');
return;
}
toastification.show(
type: ToastificationType.success,
style: ToastificationStyle.flatColored,
@@ -35,7 +50,7 @@ class Toaster {
description: Text(message),
autoCloseDuration: autoCloseDuration,
borderRadius: borderRadius,
closeButtonShowType: CloseButtonShowType.none,
closeButton: ToastCloseButton(showType: CloseButtonShowType.none),
alignment: alignment,
animationDuration: animationDuration,
pauseOnHover: false,
@@ -46,6 +61,13 @@ class Toaster {
}
static void info(String title, String message) {
print('[TOAST]: [[INFO]] $title:\n$message');
if (!Globals().appWidgetInitialized) {
print('[TOAST]: Cannot show info-toast - app<widget> not initialized - skipping');
return;
}
toastification.show(
type: ToastificationType.info,
style: ToastificationStyle.flatColored,
@@ -53,7 +75,7 @@ class Toaster {
description: Text(message),
autoCloseDuration: autoCloseDuration,
borderRadius: borderRadius,
closeButtonShowType: CloseButtonShowType.none,
closeButton: ToastCloseButton(showType: CloseButtonShowType.none),
alignment: alignment,
animationDuration: animationDuration,
pauseOnHover: false,
@@ -64,6 +86,13 @@ class Toaster {
}
static void warn(String title, String message) {
print('[TOAST]: [[WARN]] $title:\n$message');
if (!Globals().appWidgetInitialized) {
print('[TOAST]: Cannot show warn-toast - app<widget> not initialized - skipping');
return;
}
toastification.show(
type: ToastificationType.warning,
style: ToastificationStyle.flatColored,
@@ -71,7 +100,7 @@ class Toaster {
description: Text(message),
autoCloseDuration: autoCloseDuration,
borderRadius: borderRadius,
closeButtonShowType: CloseButtonShowType.none,
closeButton: ToastCloseButton(showType: CloseButtonShowType.none),
alignment: alignment,
animationDuration: animationDuration,
pauseOnHover: false,
@@ -82,6 +111,13 @@ class Toaster {
}
static void error(String title, String message) {
print('[TOAST]: [[ERROR]] $title:\n$message');
if (!Globals().appWidgetInitialized) {
print('[TOAST]: Cannot show error-toast - app<widget> not initialized - skipping');
return;
}
toastification.show(
type: ToastificationType.error,
style: ToastificationStyle.flatColored,
@@ -89,7 +125,7 @@ class Toaster {
description: Text(message),
autoCloseDuration: autoCloseDuration,
borderRadius: borderRadius,
closeButtonShowType: CloseButtonShowType.none,
closeButton: ToastCloseButton(showType: CloseButtonShowType.none),
alignment: alignment,
animationDuration: animationDuration,
pauseOnHover: false,