2

Minimum reproducible code:

void main() {
  // All type of initialization is done. 

  // Throws null exception. 
  FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async {
    print(message);
  });

  // Works.  
  FirebaseMessaging.onBackgroundMessage(_foo);
}

Future<void> _foo(RemoteMessage message) async {
  print(message);
}

Here's the error:

E/flutter (14716): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception:
                   Null check operator used on a null value
E/flutter (14716): #0      MethodChannelFirebaseMessaging.registerBackgroundMessageHandler 
                          (package:firebase_messaging_platform_interface/src/method_channel/method_channel_messaging.dart:179:53)
E/flutter (14716): #1      FirebaseMessagingPlatform.onBackgroundMessage= (package:firebase_messaging_platform_interface/src/
                          platform_interface/platform_interface_messaging.dart:101:16)
E/flutter (14716): #2      FirebaseMessaging.onBackgroundMessage (package:firebase_messaging/src/messaging.dart:83:31)

I don't understand how come there be a null exception on using an anonymous function? Although docs states that this should not be an anonymous function but a null exception for that or am I misinterpreting something?

iDecode
  • 22,623
  • 19
  • 99
  • 186

1 Answers1

0

the cloud messaging docs:

There are a few things to keep in mind about your background message handler:

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

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. You can however perform logic such as HTTP requests, IO operations (updating local storage), communicate with other plugins etc.

cas
  • 757
  • 4
  • 19
  • Sorry but it doesn't really answer the question, since my question is more related to null than the API and I've already mentioned this point in my post, so your answer technically didn't bring any value on the table. – iDecode Aug 10 '21 at 15:11
  • hmmyeah I see your point, according to the docs It should throw an argumentException, but I can't find any reference to that in the source. I do see what's causing the null check operator error though. personally i wouldn't know how to distinguish an anonymous function in code anyway, I dont think they have a seperate type – cas Aug 10 '21 at 16:56
  • anyhow this is probably non expected behaviour and warrants an issue on Github – cas Aug 10 '21 at 17:02
  • "I do see what's causing the null check operator error though." -- What's causing it? – iDecode Aug 11 '21 at 05:32
  • the callback is supposed to be assigned to a nullable variable [here](https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging_platform_interface/lib/src/platform_interface/platform_interface_messaging.dart), and is later called and inforced as not null [here](https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging_platform_interface/lib/src/method_channel/method_channel_messaging.dart) – cas Aug 11 '21 at 10:15