1

I'm trying to fetch data from Cloud Firestore on flutter but as soon as I import the cloud_firestore.dart package, the app doesn't even build and crashes instantly with a lot of errors.

https://hastebin.com/esuzizuzod.sql this is the error it throws.

And this is the code

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

Future<void> main() async {
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Firestore Demo"),
      ),
      body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection("users").snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Text("Loading data...Please wait!");
            } else {
              return Column(
                children: <Widget>[
                  Text(snapshot.data.documents[0]["name"]),
                  Text(snapshot.data.documents[0]["age"]),
                  Text(snapshot.data.documents[0]["role"]),
                ],
              );
            }
          }),
    );
  }
} 

I have also implemented all the packages in the pubspec.yaml file.

Does anyone know how to fix this?

Thanks

Leo
  • 11
  • 3
  • Hi, please read all 3 duplicates, in the first link as you can see you have to call WidgetsFlutterBinding.ensureInitialized(); before await Firebase.initializeApp() , the other links will help you regarding firestore.. Also execute `flutter pub cache repair`, add the packages again to your pubspec.yaml file save, close the ide and open it again – Peter Haddad Aug 24 '20 at 09:38
  • 1
    Thank you so much @PeterHaddad you saved my life – Leo Aug 24 '20 at 19:12

0 Answers0