Getting Error screen while data loading Flutter/Dart/FireBase

Issue

I have 3 dart files in my page . I have 2 page.
When i get data from firebase for second page , im getting a error screen for a second than my data loading. I checked error message it says my data null.
I tried Circular progress indicator like this

return StreamBuilder(
    stream: Firestore.instance.collection(UserInformation).document(user.uid).snapshots(),
    builder: (context, snapshot) {
    //==========show progress============
     if (!snapshot.hasData) {
        return CircularProgressIndicator()            
      }
      var userDocument = snapshot.data;
      // User userDocument here ..........

but it didn’t work.

This is the area which i get my data from firebase;


class GuessPage extends StatefulWidget {
  late String topicname;
  GuessPage({required this.topicname});
  setType(String topic) {
    var questionsOf;
    CollectionReference questionsRef =  FirebaseFirestore.instance.collection('questionSports');
    if (topic=='Sports'){
       questionsOf =  questionsRef.doc('questionS');
    }else if(topic=='Magazine'){
       questionsOf =  questionsRef.doc('questionM');
    }
    print(questionsOf);
    return questionsOf;
  }

  @override
  State<GuessPage> createState() => _GuessPageState();
}

and this is my main dart file which i initialize my firebase

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData.dark(),
        home: FutureBuilder(
            future: _initialization,
            builder: (context, snapshot) {
              if (snapshot.hasError) {
                return Center(
                  child: Text('Unexpected Error'),
                );
              } else if (snapshot.hasData) {
                return InputPage();
              } else {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            }));
  }
}

Solution

I guess you’re using an old version of the Firestore package.

Get the latest version of Firestore from pub.dev, right here: https://pub.dev/packages/cloud_firestore

And then check usage of the package from here: https://firebase.flutter.dev/docs/firestore/usage

Answered By – Sreelal TS

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *