简体   繁体   English

数据加载 Flutter/Dart/FireBase 时出现错误屏幕

[英]Getting Error screen while data loading Flutter/Dart/FireBase

I have 3 dart files in my page.我的页面中有 3 个 dart 文件。 I have 2 page.我有 2 页。 When i get data from firebase for second page, im getting a error screen for a second than my data loading.当我从 firebase 获取第二页的数据时,我得到的错误屏幕比我的数据加载要多一秒。 I checked error message it says my data null.我检查了错误消息,上面写着我的数据 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;这是我从 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这是我的主要 dart 文件,我初始化我的 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(),
                );
              }
            }));
  }
}

I guess you're using an old version of the Firestore package.我猜您使用的是旧版本的 Firestore package。

Get the latest version of Firestore from pub.dev, right here: https://pub.dev/packages/cloud_firestore从 pub.dev 获取最新版本的 Firestore,就在此处: https://pub.dev/packages/cloud_firestore

And then check usage of the package from here: https://firebase.flutter.dev/docs/firestore/usage然后从这里检查 package 的使用情况: https://firebase.flutter.dev/docs/firestore/usage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM