简体   繁体   English

在从 flutter 中的 Firestore 加载数据之前出现错误

[英]getting an error before loading data from Firestore in flutter

Here when i try to load data in my flutter app, before loading data it throws an error for 1 second then it displays the data, here is the error:在这里,当我尝试在我的 flutter 应用程序中加载数据时,在加载数据之前它会抛出错误 1 秒钟然后显示数据,这是错误: 在此处输入图像描述

here is my code:这是我的代码:

  class MapScreenState extends State<DashboardPage> {

  Stream<DocumentSnapshot> getData() async*{
    var user = await FirebaseAuth.instance.currentUser();
    yield* Firestore.instance.collection('Profile').document(user.uid).snapshots();
  }
  @override
  void initState() {
    super.initState();

    getData();//until this is completed user stays null we can use it to check whether it's loaded
  }

  @override
  Widget build(BuildContext context) {

    return StreamBuilder(
      stream: getData(),
      builder: (context, snapshot) {

        if (snapshot.hasError) {
          return new showProfile(
            name: "anonymous",
            email: "null",
            pin: "null",
            address: "null",
          );
        }else {
          var userDocument = snapshot.data;
          return new showProfile(
            name: userDocument['name'],
            email: userDocument["email"],
            pin: userDocument["pin"],
            address: userDocument["address"],
          );
        }

      },
    );
  }
}

How can i retrieve the data without showing the error?如何在不显示错误的情况下检索数据?

Somewhere in your code there is a Text widget that is being given a string from firestore before the string is loaded.在您的代码中的某处,有一个Text小部件在加载字符串之前从 firestore 获得了一个字符串。 Try doing this:尝试这样做:

...

Text(stringFromFirestore ?? ''),

...

In dart, the ??在 dart 中, ?? operator checks if the first value (stringFromFirestore) is null, and if it is not, it returns the first value, but if it is, it returns the second value.运算符检查第一个值(stringFromFirestore)是否为 null,如果不是,则返回第一个值,如果是,则返回第二个值。

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

相关问题 从 StreamBuilder BloC firestore Flutter 获取数据时出错 - Error when getting data from StreamBuilder BloC firestore Flutter 将数据从Firestore加载到带有抖动的列表中的问题 - Problem with loading data from firestore into a list with flutter Flutter-循环从Firestore获取数据 - Flutter - Getting data from firestore in a loop 尝试在使用 Flutter 从 Firestore 加载数据时实现加载微调器 - Trying to implement loading spinner while loading data from Firestore with Flutter 将数据从 Firestore 提取到 flutter 流时出错 - Error pulling data from firestore to flutter streams 没有错误,但我在使用 GetX flutter 时没有从我的 Firestore 数据库中获取数据 - There's no error but I am not getting data from my firestore database while using GetX flutter 我在尝试将数据从云 Firestore 带到我的 flutter 应用程序时遇到此错误 - I am getting this error while am trying to bring data from cloud firestore to my flutter app 我想在 flutter 中从 firestore 获取此数据后对数据进行洗牌 - I want to shuffle the data after getting this data from firestore in flutter 将日期数据上传到 flutter 中的 firestore 时出现错误 - I am getting error while uploading date data to firestore in flutter 试图从 firestore 获取数据但出现此错误 - trying to get data from firestore but getting this ERROR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM