getx icon indicating copy to clipboard operation
getx copied to clipboard

The getter 'page' was called on null - When receiving FCM Notification (App terminated)

Open odcey opened this issue 4 years ago • 3 comments

Describe the bug I've got an error which seems related to the getPages from GetMaterialApp But the error occurs ONLY when the app is opened from a FCM Notification that contains "data" attribute.

If i remove the "data" attribute the app opens fine. But i need this data to redirect the app in the right Page when user opens the notification. This woks when app is in background.

I put some logs in the app (can't set a breakpoint because i'm testing the notification when app terminated) I can see that my services are called. But it crashes on getPages.

This code works fine but only in this precise scenario, the app crashes at launch.

Reproduction code

main.dart


void main() async {
  // Initialize services (method below)
  await initServices();
  
  
  runApp(
      GetMaterialApp(
        // Where the problem occurs 
        getPages: Routes.getRoutes(),
        home: SplashScreenPage(),
        // Other attributes (also tried to add 'initialRoute'
      ),
    );
  
  /// The method i use to initialize some services (Hive db / Firebase ...)
  Future<void> initServices() async {
      // This is a custom service when i handle the notification when i receive it (onResume, onLaunch ...)
      // But the problem occurs even if i remove this service
      Get.put(FirebaseNotificationsService());
      // Some other services (with putAsync)
  }
}

routes.dart

class Routes {
  static List<GetPage> getRoutes() {
    return [
      // Home route
      GetPage(
        name: '/home',
        page: () => MainTabAppLayout(),
      ),
      // ... More routes
    }
}

FCM Notification payload:

{
    "notification": {
        "body": "this is a body",
        "title": "this is a title"
    },
    "priority": "high",
    
    "data": {
        "click_action": "FLUTTER_NOTIFICATION_CLICK",
        "route": "recipes"
    },
    "to": "token"
}

To Reproduce Steps to reproduce the behaviour:

  1. Install firebase_messaging: ^7.0.3 on Android (by following the steps in the firebase_messaging Readme)
  2. Use getPages as i did
  3. Open your app normally and check everything works fine
  4. Now kill the app, send a push notification (with Postman) And open your app by clicking this notification
  5. You'll get the error The getter 'page' was called on null.

Expected behaviour Expected to have the same behaviour as when the app opens without a notification

Flutter Version:

Flutter version 1.22.5
Framework revision 7891006299 (9 weeks ago), 2020-12-10 11:54:40 -0800
Dart version 2.10.4

Getx Version: get: ^3.25.0

Describe on which device you found the bug: OnePlus6

odcey avatar Feb 14 '21 18:02 odcey

Hey, I assume that you run a logic to redirect to the right page after receiving an FCM notification message while the app is in the background. Maybe check over there (in the redirection logic) if everything is initialized properly, because The getter 'page' was called on null. may not refer to GetPages, but also to an object with a page property. Also, you cannot perform UI task on the background handler and it must be a global function. Check that too.

kamazoun avatar Feb 27 '21 18:02 kamazoun

Yeah, maybe the 'page' refers to another part of code. Just to be more specific, you mention 'when app is in background' . When this is the case it works fine. My problem is when the app is "terminated" Thank you i'll try your advices

odcey avatar Mar 03 '21 11:03 odcey

@odcey any updated? i have same issue. My problem is when app is TERMINATED, and then The getter 'page' was called on null

rsyd29 avatar Jun 22 '22 09:06 rsyd29