【发布时间】:2020-08-04 20:02:33
【问题描述】:
在这里,当我尝试在我的颤振应用程序中加载数据时,在加载数据之前它会抛出一个错误 1 秒钟,然后它会显示数据,这是错误:
这是我的代码:
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"],
);
}
},
);
}
}
如何在不显示错误的情况下检索数据?
【问题讨论】:
-
不是修复方法,但最好将流初始化为变量并在
build中使用它,而不是在build中调用getData()。
标签: firebase flutter google-cloud-firestore flutter-layout