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
.
No Firebase App '[DEFAULT]' has been created
firebase throws when it's not gettinggoogle-services.json
or initialized manually. – Ichneumonindex.html
. because this errors are mainly caused in import statements – ConvenanceuploadUserData
. – Transgressionawait Firebase.initializeApp();
beforefirebase_storage.FirebaseStorage.instance
in theWorkManager().executeTask
– Hairston