Facing 'No Firebase App has been created' while uploading to Firebase Storage in a Flutter app
Asked Answered
T

2

10

I want to collect user browsing data for offline backend analysis for a Flutter app. What is the optimal way to do that?

What I am planning to do is run a daily cron on the app that uploads it using the WorkManager package.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  Workmanager().initialize(uploadUserData, isInDebugMode: true);
  Workmanager()
      .registerOneOffTask('1', 'task1', initialDelay: Duration(seconds: 10));
  runApp(MyApp());
}

void uploadUserData() {
  Workmanager().executeTask((task, inputData) {
    firebase_storage.FirebaseStorage.instance
        .ref('/uploads/test_workmanager.txt')
        .putString('workmanager test');
    return Future.value(true);
  });
}

I am facing the following issue while running the above code

E/BackgroundWorker( 6733): errorCode: error, errorMessage: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

I/WM-WorkerWrapper( 6733): Worker result FAILURE for Work [ id=81642715-826f-4d40-b8ec-9d86eab75136, tags={ be.tramckrijte.workmanager.BackgroundWorker } ]

I have checked in multiple ways that firebase is initialized before calling the workmanager task (using firebase.apps.length & .whenComplete)

Update: Many are doubting whether firebase connection is right: basically everything just works if I just call firebase_storage....putString in the main() instead of within Workmanager().executeTask. So, the trouble is only due to Workmanager.

Transgression answered 11/9, 2021 at 12:44 Comment(10)
Verify whether android implementation in the flutter project is done. No Firebase App '[DEFAULT]' has been created firebase throws when it's not getting google-services.json or initialized manually.Ichneumon
@ArulMani: My Firebase Auth, Firestore, Storage and other services have been working and thoroughly tested. It's just that this WorkManager thing is giving issue.Transgression
If you have updated your firebase version to v9 then this might give you the error as the new update is using modular style which flutter hasnt yet implemented for firebase integration and it would be more helpful to check for error cause if you upload your index.html. because this errors are mainly caused in import statementsConvenance
@Mithson: I am running this on Android not web; I am not getting this error in an import statement but in the WorkManager task uploadUserData.Transgression
did you connect your app to firebase properly? where is your service.json file?Milly
@SaifulIslam: Yes, firebase connection is alright. Please check the update.Transgression
I have opened an issue at this repo's GitHub: github.com/fluttercommunity/flutter_workmanager/issues/315Transgression
Do you tried calling await Firebase.initializeApp(); before firebase_storage.FirebaseStorage.instance in the WorkManager().executeTaskHairston
@J.MARS Yes I had tried that, it didn't work. Same suggestion was given to me in the above github issue.Transgression
Did you ever figure this out? I've had nothing but trouble with Workmanager and these sorts of issues.Redeem
M
5
  1. First call await Firebase.initializeApp(); in Workmanager().executeTask() async { }.

  2. I recommend also "awaiting" all the following Firebase actions in the Workmanager task: await firebase_storage.FirebaseStorage.instance.[...]. This is because without await, the Workmanager might consider the task finished and kill the process before the Firebase action finishes.

Full Workmanager example task code with changes:

Workmanager().executeTask((task, inputData) async {
  await Firebase.initializeApp();
  await firebase_storage.FirebaseStorage.instance
      .ref('/uploads/test_workmanager.txt')
      .putString('workmanager test');
  return Future.value(true);
});
Millikan answered 20/3, 2022 at 12:45 Comment(1)
I had the same issue and it works.Pentachlorophenol
B
1

You can call Firebase.initializeApp(); again in the Workmanager().executeTask() method before invoking any Firebase call.

Birthroot answered 4/2, 2022 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.