Short answer
Call WidgetsFlutterBinding.ensureInitialized(), then await Firebase.initializeApp() with DefaultFirebaseOptions.currentPlatform, all before runApp. If the crash happens when a message arrives in the background, call Firebase.initializeApp() inside the background message handler too, as Firebase’s own example does.
Where Firebase wasn’t initialized, and the fix
01
main() doesn’t await initializeApp before runApp
“Also in your lib/main.dart file, initialize Firebase using the DefaultFirebaseOptions object exported by the configuration file:” Firebase, Add Firebase to your Flutter app
Any Firebase call before initialization finishes fails with this error, including one in a widget that builds on the first frame. Make main async and initialize first, exactly as Firebase’s setup guide shows:
WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); runApp(const MyApp());02
The background message handler doesn’t initialize
“Since the handler runs in its own isolate outside your applications context, it is not possible to update application state or execute any UI impacting logic.” Firebase, Receive messages in a Flutter app
On Android, a message that arrives while the app is in the background runs your handler in its own isolate, which doesn’t share the setup done in main. Firebase’s own example initializes at the top of the handler:
@pragma('vm:entry-point') Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { // If you're going to use other Firebase services in the background, such as Firestore, // make sure you call `initializeApp` before using other Firebase services. await Firebase.initializeApp(); print("Handling a background message: ${message.messageId}"); }03
The configuration file is out of date
We also check that firebase_options.dart matches the project and platforms you build for. Firebase’s guide says to run flutterfire configure so the configuration stays up to date, then rebuild.
Checked against the vendor’s docs on : Firebase docs: Add Firebase to your Flutter app; Firebase docs: Receive messages in a Flutter app.
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way.See scope and start
Related: Flutter APNS token has not been set · Flutter pod install failed · Unexpected Gemini API charges on Firebase · Firestore quota exceeded