skip to Main Content

I just created a new flutter application and trying to connect a newly made firebase Firestore. Cli has been added and flutterfire config has been ran.

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform
  );
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FutureBuilder(builder: ((context, snapshot) {
        if(snapshot.hasError){
          print('Error');
        }
        if(snapshot.connectionState == ConnectionState.done){
          return const MyHomePage(title: 'WOWZA',);
        }
        return CircularProgressIndicator();
      }
      )
      )
    );
  }
}

When breaking at the if(snapshot.connectionState == ConnectionState.done) is says connectionState is .none and unsure as to why.

Can anyone help?

2

Answers


  1. Chosen as BEST ANSWER

    missing future initialization -

    From:

    home: FutureBuilder(builder: ((context, snapshot) {
    
        if(snapshot.hasError){
          print('Error');
        }
        if(snapshot.connectionState == ConnectionState.done){
          return const MyHomePage();
        }
        return CircularProgressIndicator();
      }
      )
      )

    To:

    home: FutureBuilder(
            future: _initialization,
            builder: ((context, snapshot) {
            if(snapshot.hasError){
              print('Error');
            }
            if(snapshot.connectionState == ConnectionState.done){
              return const MyHomePage();
            }
            return CircularProgressIndicator();
          }
          )
          )


  2. Modify your code with this snippet.

    import 'package:flutter/material.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'firebase_options.dart';
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: FutureBuilder(
            future: Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform
      ),
            builder: ((context, snapshot) {
            if(snapshot.hasError){
              print('Error');
            }
            if(snapshot.connectionState == ConnectionState.done){
              return const MyHomePage(title: 'WOWZA',);
            }
            return CircularProgressIndicator();
          }
          )
          )
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search