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

后端 未结 9 669
慢半拍i
慢半拍i 2020-12-01 15:55

I have a firebase database linked up to two apps, one being an iOS app and another being a web app coded in node.js which is a basic algorithm that sets data to the database

9条回答
  •  独厮守ぢ
    2020-12-01 15:56

    add async in main()

    and await before

    follow my code

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      var fsconnect = FirebaseFirestore.instance;
    
      myget() async {
        var d = await fsconnect.collection("students").get();
        // print(d.docs[0].data());
    
        for (var i in d.docs) {
          print(i.data());
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            home: Scaffold(
          appBar: AppBar(
            title: Text('Firebase Firestore App'),
          ),
          body: Column(
            children: [
              RaisedButton(
                child: Text('send data'),
                onPressed: () {
                  fsconnect.collection("students").add({
                    'name': 'sarah',
                    'title': 'xyz',
                    'email': 'sarah@gmail.com',
                  });
                  print("send ..");
                },
              ),
              RaisedButton(
                child: Text('get data'),
                onPressed: () {
                  myget();
                  print("get data ...");
                },
              )
            ],
          ),
        ));
      }
    }
    

提交回复
热议问题