Reputation: 1
I initialized Firebase inside main, and I need to access user data inside workmanager to provide the user with personalized notification, but workmanager for some reason works synchronously at the same time as Firebase initialization, so it caused this problem.
I tried to add a simple if statement like this to solve the problem and add the initialization twice, one for the main and one for the work manager, but it did not solve the issue.
The output was start firebase initialization printed twice and this error: lStateException: FirebaseApp name [DEFAULT] already exists!, IllegalStateException, Cause: null, Stacktrace: java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!
// AuthService code
// initialize firebase
static Future initializeFirebase() async {
if (!isInitialized && Firebase.apps.isEmpty) {
print('start firebase initializion');
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
isInitialized = true;
} else {
print('firebase is already initialized');
}
}
...
/////////////////////////////////////////////////////////////
//here is main code:
Future<void> main() async {
// initialize firebase
WidgetsFlutterBinding.ensureInitialized();
await AuthService.initializeFirebase();
// preferred screen orientations
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
// initialize notifications
NotificationService().initNotification();
//initialize workmanager
Workmanager().initialize(callbackDispatcher);
...
void callbackDispatcher() async {
print('entered...');
// initialize firebase
WidgetsFlutterBinding.ensureInitialized();
await AuthService.initializeFirebase();
//////////// access data for notification ////////////////
final user = FirebaseAuth.instance.currentUser;
String uid = user!.uid;
//child collection
final CollectionReference childCollection =
FirebaseFirestore.instance.collection('child');
//morning collection
CollectionReference schedule_morning =
childCollection.doc(uid).collection('schedule_morning');
final QuerySnapshot morning_tasks = await schedule_morning.get();
//noon collection
CollectionReference schedule_noon =
childCollection.doc(uid).collection('schedule_noon');
final QuerySnapshot noon_tasks = await schedule_noon.get();
//night collection
CollectionReference schedule_night =
childCollection.doc(uid).collection('schedule_night');
final QuerySnapshot night_tasks = await schedule_night.get();
//to get user name
final DocumentSnapshot userDoc = await childCollection.doc(uid).get();
if (userDoc.exists) {
Map<String, dynamic> userData = userDoc.data() as Map<String, dynamic>;
final String userName = userData['name'];
// notify the user
if (DateTime.now().hour == 06 && morning_tasks.size > 0) {
Workmanager().executeTask((taskName, inputData) {
NotificationService().showNotification(
title: 'todays tasks',
body: '$userName do not forget your morning tasks',
);
return Future.value(true);
});
}
...
Upvotes: 0
Views: 187
Reputation: 2236
UPDATED ANSWER
Can you try by initializing Firebase in the executeTask method like below.
Workmanager().executeTask((task, inputData) async {
await Firebase.initializeApp();
return Future.value(true);
});
OLD ANSWER
How to setup up Firebase using FlutterFire?
First thing is that you do not have to manually check if firebase is initialized. So just call this function once.
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
To fix your issue run these commands from your project root folder,
flutter pub add firebase_core firebase_messaging
# Install the CLI if not already done so
dart pub global activate flutterfire_cli
# Run the `configure` command, select a Firebase project and platforms
flutterfire configure
For more information visit this link.
Upvotes: 0