Why GetIt Library Needs Re-Init While App In Background(Not terminated)?

Viewed 203

I'm trying to handle push notification cases like receiving messages, tapping notification etc. The problem happens when calling registered modules from GetIt while app in background(not terminated)

The simple scenario is,

  • Open the app
  • Tap home button and push the app to background
  • Send push notification to device
  • Then below method, onBackgroundMessage is triggered

locator retrieves GeneralRepository module

Future<void> onBackgroundMessage(RemoteMessage message) async {
  try {
    await Firebase.initializeApp();
    var repo = locator<GeneralRepository>(); ---> BOOM! THIS LINE WHERE THE EXCEPTION OCCURS
    var user = await repo.getUser(); 
    showNotification(message);
  } catch (e) {}
}

And error logs,

Object/factory with  type GeneralRepository is not registered inside GetIt. 
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)

I found this SO answer and it works. It's saying, just re-init locator modules again.

But why should we do this ? App is not terminated!

0 Answers
Related