WidgetsFlutterBinding.ensureInitialized() error in custom code (implementing push notification with supabase and firebase cloud messaging)

Custom Code

Hey all,

I'm facing an using fllowing this tutorial in order to implement push notification in my app : https://www.youtube.com/watch?v=IHQJ5P3PR9U&ab_channel=justxolotl

I have a custom code 'initializeMessaging' in order to ask permission to receive notifications, initialize the FCM token to the user concerned then the notification display and finally implement listener to notifications I will receive. I call this action to the final actions in main.dart. The code compile well. When I launch test mode I have an error on my browser : no screen display. You will find the log console in image attachment. I noticed that the error occurs supabase initialization in main.dart with a local_storage story and a multiple call to the WidgetsFlutterBinding.ensureInitialized() binding method but I'm now lost. I'm web developper but not used to use dart language and flutter framework for mobile app... Is that a bug from supabase integration ? Thank you for those who will help ๐Ÿ™

Below my main.dart and initializeMessaging action.

main.dart :

import '/custom_code/actions/index.dart' as actions;

import 'package:provider/provider.dart';

import 'package:flutter/gestures.dart';

import 'package:flutter/material.dart';

import 'package:flutter_localizations/flutter_localizations.dart';

import 'package:flutter_web_plugins/url_strategy.dart';

import 'auth/supabase_auth/supabase_user_provider.dart';

import 'auth/supabase_auth/auth_util.dart';

import '/backend/supabase/supabase.dart';

import 'backend/firebase/firebase_config.dart';

import '/flutter_flow/flutter_flow_theme.dart';

import 'flutter_flow/flutter_flow_util.dart';

import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'flutter_flow/nav/nav.dart';

import 'index.dart';

void main() async {

WidgetsFlutterBinding.ensureInitialized();

GoRouter.optionURLReflectsImperativeAPIs = true;

usePathUrlStrategy();

final environmentValues = FFDevEnvironmentValues();

await environmentValues.initialize();

await initFirebase();

await SupaFlow.initialize();

final appState = FFAppState(); // Initialize FFAppState

await appState.initializePersistedState();

// Start final custom actions code

await actions.initializeMessaging();

// End final custom actions code

runApp(ChangeNotifierProvider(

create: (context) => appState,

child: MyApp(),

));

}

initializeMessaging.dart :

// Automatic FlutterFlow imports

import '/backend/supabase/supabase.dart';

import '/flutter_flow/flutter_flow_theme.dart';

import '/flutter_flow/flutter_flow_util.dart';

import '/custom_code/actions/index.dart'; // Imports other custom actions

import '/flutter_flow/custom_functions.dart'; // Imports custom functions

import 'package:flutter/material.dart';

// Begin custom action code

// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:collection/collection.dart';

import 'package:firebase_core/firebase_core.dart';

import 'package:firebase_messaging/firebase_messaging.dart';

import 'package:awesome_notifications/awesome_notifications.dart';

Future initializeMessaging() async {

// Add your function code here!

await Firebase.initializeApp();

FirebaseMessaging.instance.requestPermission().then((settings) {

if (settings.authorizationStatus == AuthorizationStatus.authorized) {

FirebaseMessaging.instance.getToken().then((token) {

FFAppState().fcmToken = token ?? "No token";

});

} else {

FFAppState().fcmToken = "No token";

}

});

// Initialize our local notifications

await AwesomeNotifications().initialize(

null,

[

NotificationChannel(

channelKey: 'alerts',

channelName: 'Alerts',

channelDescription: 'Notification tests as alerts',

playSound: true,

onlyAlertOnce: true,

groupAlertBehavior: GroupAlertBehavior.Children,

importance: NotificationImportance.High,

defaultPrivacy: NotificationPrivacy.Private,

defaultColor: Colors.deepPurple,

ledColor: Colors.deepPurple,

)

],

debug: true,

);

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {

print("received message 1: ${message.notification?.title}");

await AwesomeNotifications().createNotification(

content: NotificationContent(

id: -1,

channelKey: 'alerts',

title: message.notification?.title ?? "No title",

body: message.notification?.body ?? "No body",

payload: {'notificationId': '1234567890'}),

actionButtons: [

NotificationActionButton(

key: "DISMISS",

label: "Dismiss",

actionType: ActionType.DismissAction,

isDangerousOption: true,

)

]);

});

}

What have you tried so far?

I can't modify main.dart so I'm stuck. I tried to call my action in initial actions but same error. I also tried in a new project with supabase integration and same error. I haven't try to download my code in vs code in order to test with terminal...

My fcmToken in app state exists and firebase_messaging and awesome_notifications are well integrated

Did you check FlutterFlow's Documentation for this topic?
Yes