I am working on a project and encountered a problem. I have already initialised Firebase in the main.dart as so:
class _AppState extends State<App> {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
return FutureBuilder(
// Initialize FlutterFire:
future: _initialization,
builder: (context, snapshot) {
if (snapshot.hasError) {}
if (snapshot.connectionState == ConnectionState.done) {
return MaterialApp(
routes: appRoutes,
theme: appTheme,
);
}
// return const MaterialApp(home: LoadingScreen());
return const Directionality(
textDirection: TextDirection.ltr, child: Text("Loading"));
},
);
}
Everything well so far, the app runs and firebase initialises as it should. Routs set like this:
'/': (context) => const HomePage(),
'/login': (context) => const LoginPage(),
So, we will naturally go to the home page. Here is where the error comes in. Here in the home page, I want to listen at a stream if the user is logged in or not. The problem is that when I initialise the stream I get the following error: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
Here is the code of the home page:
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: AuthService().userStream,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("loading");
} else if (snapshot.hasError) {
return const Center(
child: Text("Error"),
);
} else if (snapshot.hasData) {
return const ClientDashboardPage();
} else {
print("nodata");
return const LoginPage();
}
},
);
}
}
And here is the Auth File:
import 'package:firebase_auth/firebase_auth.dart';
class AuthService {
final userStream = FirebaseAuth.instance.authStateChanges();
final user = FirebaseAuth.instance.currentUser;
}
So all I am doing is listening to the stream and that error is thrown. If I remove the whole home page and just put a text, I see the text. So the error is clearly from the Auth Stream. I have searched online about this error but the most majority say that this is due to firebase not being initialised. If it was not in my case, I would not even come to the home page since this is being checked in the main.dart.
What solutions / ideas do you guys have? Thank you so much for the help!
Here is the full error:
════════ Exception caught by widgets library ═══════════════════════════════════
The following FirebaseException was thrown building HomePage(dirty):
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
The relevant error-causing widget was
HomePage
routes.dart:7
When the exception was thrown, this was the stack
#0 MethodChannelFirebase.app
method_channel_firebase.dart:193
#1 Firebase.app
firebase.dart:56
#2 FirebaseAuth.instance
firebase_auth.dart:44
#3 new AuthService
auth.dart:4
#4 HomePage.build
home.dart:12
#5 StatelessElement.build
framework.dart:5367
#6 ComponentElement.performRebuild
framework.dart:5297
#7 Element.rebuild
framework.dart:5016
#8 ComponentElement._firstBuild
framework.dart:5279
#9 ComponentElement.mount
framework.dart:5273
... Normal element mounting (220 frames)
#229 Element.inflateWidget
framework.dart:4182
#230 MultiChildRenderObjectElement.inflateWidget
framework.dart:6569
#231 MultiChildRenderObjectElement.mount
framework.dart:6581
... Normal element mounting (435 frames)
#666 Element.inflateWidget
framework.dart:4182
#667 Element.updateChild
framework.dart:3701
#668 ComponentElement.performRebuild
framework.dart:5322
#669 StatefulElement.performRebuild
framework.dart:5462
#670 Element.rebuild
framework.dart:5016
#671 BuildOwner.buildScope
framework.dart:2779
#672 WidgetsBinding.drawFrame
binding.dart:916
#673 RendererBinding._handlePersistentFrameCallback
binding.dart:360
#674 SchedulerBinding._invokeFrameCallback
binding.dart:1297
#675 SchedulerBinding.handleDrawFrame
binding.dart:1227
#676 SchedulerBinding._handleDrawFrame
binding.dart:1085
#677 _invoke (dart:ui/hooks.dart:170:13)
I discovered that adding options: DefaultFirebaseOptions.currentPlatform,
solved the issue
final Future<FirebaseApp> _initialization = Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);