Search code examples
flutterblocflutter-blocflutter-ui

Flutter error - No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()


https://gist.github.com/ArpanMondalGITHUB/7012a4af48bed19d64eefc325fc23901

I am trying to sign up using Bloc and Firebase, but whenever I connect the BlocConsumer to the sign-up page form it gives me an error:

(elided 3 frames from class _Timer and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
Each child must be laid out exactly once.
The relevant error-causing widget was:
════════════════════════════════════════════════════════════════════════════════
D/MAGT_SYNC_FRAME(30444): MAGT Sync: MAGT is not supported. Disabling Sync.
D/hw-ProcessState(30444): Binder ioctl to enable oneway spam detection failed: Invalid argument
D/BLASTBufferQueue(30444): [SurfaceView[com.example.khanakhazana/com.example.khanakhazana.MainActivity]#1](f:0,a:1) acquireNextBufferLocked size=1080x2400 mFrameNumber=1 applyTransaction=true mTimestamp=11712576456697(auto) mPendingTransactions.size=0 graphicBufferId=130755984359424 transform=0
D/SurfaceView(30444): 137541013 handleSyncNoBuffer
D/SurfaceView(30444): 137541013 finishedDrawing
D/SurfaceView(30444): 137541013 performDrawFinished mAttachedToWindow true
D/VRI[MainActivity](30444):  debugCancelDraw  cancelDraw=false,count = 161,android.view.ViewRootImpl@c672b31
I/SurfaceControl(30444):  setExtendedRangeBrightness sc=Surface(name=com.example.khanakhazana/com.example.khanakhazana.MainActivity)/@0x1417180,currentBufferRatio=1.0,desiredRatio=1.0
D/SurfaceView(30444): 137541013 dispatchDraw mDrawFinished true isAboveParent false (mPrivateFlags & PFLAG_SKIP_DRAW) 128
D/SurfaceView(30444): 137541013 clearSurfaceViewPort mCornerRadius 0.0
D/BLASTBufferQueue(30444): [VRI[MainActivity]#0](f:0,a:1) acquireNextBufferLocked size=1080x2400 mFrameNumber=1 applyTransaction=true mTimestamp=11712589232005(auto) mPendingTransactions.size=0 graphicBufferId=130755984359425 transform=0

Connecting to VM Service at ws://127.0.0.1:56253/QNsrAPZw8mQ=/ws D/CompatibilityChangeReporter(30444): Compat change id reported: 3400644; UID 10293; state: DISABLED 
════════ Exception caught by widgets library ═══════════════════════════════════ 
The following FirebaseException was thrown building KeyedSubtree-[GlobalKey#a49a5]: 
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() 

The relevant error-causing widget was: 
Scaffold Scaffold:file:///C:/Users/Arpan%20Mondal/food/khanakhazana/lib/features/auth/presentation/pages/signup_page.dart:32

Solution

  • This is the main() function in your code:

    void main() async {
      runApp(MultiBlocProvider(
        providers: [
          BlocProvider(
            create: (_) => AuthBlocBloc(
              userSignup: UserSignup(
                AuthRepositoryImpl(
                  AuthRemoteDataSourceImpl(firebaseAuth: FirebaseAuth.instance),
                ),
              ),
            ),
          ),
        ],
        child: const MyApp(),
      ));
    
      WidgetsFlutterBinding.ensureInitialized;
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
    }
    

    The error says that No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

    It seems that you are trying to use FirebaseAuth before Firebase was initialised in your app.

    To fix the problem, initialise Firebase in your app before calling the runApp() method:

    void main() async {
      // Initialise Firebase before calling runApp()
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
    
      runApp(MultiBlocProvider(
        providers: [
          BlocProvider(
            create: (_) => AuthBlocBloc(
              userSignup: UserSignup(
                AuthRepositoryImpl(
                  AuthRemoteDataSourceImpl(firebaseAuth: FirebaseAuth.instance),
                ),
              ),
            ),
          ),
        ],
        child: const MyApp(),
      ));
    }