_CastError (Null check operator used on a null value) in registerBackgroundMessageHandler could be improved #5636
Replies: 11 comments 2 replies
-
Hi @hecklatcipsoft void _bootStrapFirebase() async {
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(onBackgroundMessageHandler);
}
Future<void> onBackgroundMessageHandler(RemoteMessage message) async {
print("Handling a background message ${message.data}");
//You need to do everything in here
//If you need to do anything with firebase you also need to call
//await Firebase.initializeApp();
} Thank you |
Beta Was this translation helpful? Give feedback.
-
Hi @markusaksli-nc, Thanks for your answer, I didn't read the comment that clearly states that it has to be a top level function. However you still might want to update the comment because there also isn't an ArgumentError we could handle to deal with a problem. The async registration just fails and the log is lost in the debug console. Changing registerBackgroundMessageHandler to not be async might help mitigate the problem because then at least some exception hits the consumer.
|
Beta Was this translation helpful? Give feedback.
-
I'm pretty sure MethodChannels are strictly async so you cannot delegate synchronous calls in Flutter right now so I'm not sure that would be possible. Though maybe there could be better feedback here. |
Beta Was this translation helpful? Give feedback.
-
I can agree that this message is very confusing. Also, the whole concept is a bit unclear. This callback is needed to be able to show local notifications somehow. It's not possible to pass any custom arguments into the callback, how are we supposed to show custom notifications? |
Beta Was this translation helpful? Give feedback.
-
We have spent much time trying to upgrade our packages to the latest version after upgrading flutter to V2.0.0. This issue really affects our upgrades, Is there any workaround to solve this issue? 😟 |
Beta Was this translation helpful? Give feedback.
-
@JabalSoft There is no issue. Everything works if you provide a top level named function as a callback because flutter_messaging users isolate internally. |
Beta Was this translation helpful? Give feedback.
-
Your comment really helps us, we convert our anonymous function to class function but not top-level (outside class). After we put our function outside our class OR make it static the issue solved. Maybe coloring the lines of the instructions on the documentation page with yellow or red is a good idea 😅. Thanks. |
Beta Was this translation helpful? Give feedback.
-
@AAverin What do you mean by the local notifications bit? That is what |
Beta Was this translation helpful? Give feedback.
-
I think we should improve the internal typing here so it doesn't throw a cast error at least |
Beta Was this translation helpful? Give feedback.
-
add this line "WidgetsFlutterBinding.ensureInitialized()" before "await Firebase.initializeApp()" , |
Beta Was this translation helpful? Give feedback.
-
Hi, please i still don't understand why this error persist in my code... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Bug report
Describe the bug
In File firebase_messaging_platform_interface-2.0.0\lib\src\method_channel\method_channel_messaging.dart
in function registerBackgroundMessageHandler
bgHandle and userHandle are assigned using
But PluginUtilities.getCallbackHandle can return null, which will then result in an exception:
Exception has occurred.
_CastError (Null check operator used on a null value)
Steps to reproduce
Steps to reproduce the behavior:
Expected behavior
No Exception should occur.
Additional context
Add any other context about the problem here.
Flutter doctor
Run
flutter doctor
and paste the output below:Click To Expand
Flutter dependencies
Run
flutter pub deps -- --style=compact
and paste the output below:Click To Expand
Beta Was this translation helpful? Give feedback.
All reactions