I call this to initialize Firebase.
Future initializeFirebaseApp() async {
await Firebase.initializeApp(
name: DefaultFirebaseOptions.firebaseName,
options: DefaultFirebaseOptions.currentPlatform,
)
.whenComplete(() => Log().i('Firebase Initialized'))
.onError((error, stackTrace) {
Log().e('Firebase Initialize Error: $error $stackTrace');
return Future.error("");
});
}
fun main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFirebaseApp();
runApp(const ProviderScope(child: IAmApp()));
}
And I use AutoRouter.
When I change the screen with context.pushRoute(const PasswordLoginRoute())
And PasswordLoginScreen has FirebaseAuth like this in HookConsumerWidget:
@override
Widget build(BuildContext context, WidgetRef ref) {
final firebaseAuth = ref.watch(firebaseAuthProvider);
// ...
}
@riverpod
FirebaseAuth firebaseAuth(FirebaseAuthRef ref) => FirebaseAuth.instance;
And because of that I get No Firebase App '[DEFAULT]' has been created - call
.
Should I initialize Firebase every page for web build?
It looks like you passed a custom name when initializing Firebase.
To resolve this do either one of these;
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
)
final firebaseAuth = FirebaseAuth.instanceFor(app: Firebase.app(DefaultFirebaseOptions.firebaseName));