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:
Mike Schwörer 2025-05-23 21:10:20 +02:00
parent cc13f8a0f3
commit faa624e9f8
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 56 additions and 14 deletions

View File

@ -6,26 +6,28 @@
# sudo archlinux-java set java-17-openjdk
#
java:
sudo archlinux-java set java-17-openjdk
java -version
# runs app locally (linux)
run-linux: gen
run-linux: java gen
dart run build_runner build
_JAVA_OPTIONS="" flutter run -d linux
# runs app locally (web | not really supported)
run-web: gen
run-web: java gen
dart run build_runner build
_JAVA_OPTIONS="" flutter run -d chrome
# runs on android device (must have network adb enabled teh correct IP)
run-android: gen
run-android: java gen
ping -c1 10.10.10.177
adb connect 10.10.10.177:5555
flutter pub run build_runner build
_JAVA_OPTIONS="" flutter run -d 10.10.10.177:5555
install-release: gen
install-release: java gen
# Install on Pixel 7a
flutter build apk --release
flutter run --release -d 35221JEHN07157
@ -41,7 +43,7 @@ test:
fix:
dart fix --apply
gen:
gen: java
./_utils/inc_buildnum.sh
dart run build_runner build
dart run git_stamp git_stamp --build-type lite --limit 2

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,

View File

@ -2,7 +2,7 @@ name: simplecloudnotifier
description: "Receive push messages"
publish_to: 'none'
version: 2.0.0+481
version: 2.0.0+484
environment:
sdk: '>=3.2.6 <4.0.0'