0

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.

asked Jul 23, 2022 at 22:49
4
  • 1
    Wrote this answer out a while ago, seems like your exact problem: stackoverflow.com/questions/67368311/… (read the question too). Let me know if it helps! Commented Jul 23, 2022 at 23:04
  • 1
    This also may help: stackoverflow.com/questions/63492211/… Commented Jul 23, 2022 at 23:05
  • Just for the sake of it I tried to run it on web-browser and on web browser it works normally. Then I tried the second link and just deleted and ran flutter pub add firebase_core and now it works on my emulator. Thanks Commented Jul 23, 2022 at 23:20
  • added it as an answer! Could you accept it to help people who have the same problem in the future? Commented Jul 23, 2022 at 23:36

2 Answers 2

1

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.

answered Jul 23, 2022 at 23:35
Sign up to request clarification or add additional context in comments.

Comments

0

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());
}
answered Jul 24, 2022 at 1:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.