I'm learning Flutter by following the relatively recent tutorial (made in February - LINK) and while I wrote the same code as author I'm getting the following exception:
======== Exception caught by widgets library =======================================================
The following FirebaseException was thrown building FutureBuilder<FirebaseApp>(dirty, state: _FutureBuilderState<FirebaseApp>#881ee):
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
The relevant error-causing widget was:
FutureBuilder<FirebaseApp> FutureBuilder:file:///C:/Users/Mrky/Desktop/Sve/Faks/10_semestar/mynotes/lib/main.dart:30:13
When the exception was thrown, this was the stack:
#0 MethodChannelFirebase.app (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:173:5)
#1 Firebase.app (package:firebase_core/src/firebase.dart:53:41)
#2 FirebaseAuth.instance (package:firebase_auth/src/firebase_auth.dart:38:47)
#3 HomePage.build.<anonymous closure> (package:mynotes/main.dart:37:34)
#4 _FutureBuilderState.build (package:flutter/src/widgets/async.dart:615:55)
#5 StatefulElement.build (package:flutter/src/widgets/framework.dart:4919:27)
#6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4806:15)
#7 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4977:11)
#8 Element.rebuild (package:flutter/src/widgets/framework.dart:4529:5)
#9 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
#10 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:891:21)
#11 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:370:5)
#12 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1146:15)
#13 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1083:9)
#14 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:997:5)
#18 _invoke (dart:ui/hooks.dart:151:10)
#19 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#20 _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)
==========================================================================================
The code that I have currently written is:
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:mynotes/firebase_options.dart';
import 'package:mynotes/views/login_view.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const HomePage(),
),
);
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: FutureBuilder(
future: Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.done:
print(FirebaseAuth.instance.currentUser);
return const Text('Done');
default:
return const Text('Loading...');
}
},
),
);
}
}
As it is visible I have Firebase.initializeApp inside future of FutureBuilder. Also If I comment out the line print(FirebaseAuth.instance.currentUser);
the app will show text Done without any problem.
CodePudding user response:
Run flutter clean
, then add the dart pub package firebase_core
with the command flutter pub add firebase_core
. Finally, run flutter pub get
to resolve all the dependencies.
CodePudding user response:
Make your void function async and add the following lines inside your void main.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}