[英]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.