Flutter: FCM Unhandled Exception: Null check operator used on a null value

5,017

Solution 1

I had the same error as like you, on the same line. I checked out docs and it says 2 things about background message handler.

  1. It must not be an anonymous function.
  2. It must be a top-level function (e.g. not a class method which requires initialization).

In my case it was not a top-level function, it was declared inside a class. When you move your handler out of any class or function, so that it is a top-level function and doesn't require any class or method initialisation then the error will be gone.

Solution 2

_firebaseMessagingBackgroundHandler function should be outside of the main function.

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
}


Future<void> main() async {

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);


  runApp(
    ...
  );
}

Solution 3

In my case, doing what docs said was not enough. So I realized that I should add WidgetsFlutterBinding.ensureInitialized() before everything in main function like this:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseMessaging.onBackgroundMessage(_handleMessage);
  runApp(const Homino());
}
Share:
5,017
chichi
Author by

chichi

Updated on November 20, 2022

Comments

  • chichi
    chichi over 1 year

    E/flutter (26872): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value E/flutter (26872): #0
    MethodChannelFirebaseMessaging.registerBackgroundMessageHandler (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:173:53) E/flutter (26872): #1
    FirebaseMessagingPlatform.onBackgroundMessage= (package:firebase_messaging_platform_interface/src/platform_interface/platform_interface_messaging.dart:108:16)

    // Background Messaging Set Up
        Future<void> _firebaseMessagingBackgroundHandler(
            RemoteMessage message) async {
          print('background message');
        }
    
        FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
        runApp(....)
    

    I am getting an error for this code on Android system. Everything works except when the app is terminated.

    What works on Android:

    • Notification when Terminated, onBackground and onForeground
    • Date only when onForeground

    What does not work on Android:

    • Data only when Terminated and onBackground

    What works on iOS:

    • Notification when Terminated, onBackground and onForeground
    • Date only when onForeground

    What does not work on iOS:

    • Data only when Terminated,

    I have no clue why I am getting that null value error on Android system and how can I fix this issue? Also, is it true that I can not receive the Data only push notification on iOS when the app is terminated?

  • chichi
    chichi almost 3 years
    It was from my stupidity that top-level function -> put it on top of the class.
  • chichi
    chichi almost 3 years
    Right. When I read about top-level, I assumed that I had to put it on top inside the class or function. I've realized it meant OUTSIDE.
  • Rafsan Uddin Beg Rizan
    Rafsan Uddin Beg Rizan almost 3 years
    Yes, put outside :)
  • chichi
    chichi almost 2 years
    oh yes, that too was a problem as well.