【发布时间】:2020-09-19 20:46:39
【问题描述】:
我想根据一个名为 isAdmin 的布尔变量来决定屏幕上显示的内容。此字段的值是从 Firestore 中获取的。
这是我目前得到的:
Future<bool> isUserAnAdmin() async {
var snap = await Firestore.instance
.collection("Users")
.document(UserSingleton().fireUser.uid)
.get();
UserSingleton().user = User.fromJson(snap.data);
return UserSingleton().user.isAdmin;
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
title: Text("hemlp"),
actions: [
Padding(
padding: const EdgeInsets.only(right: 15),
child: GestureDetector(
child: Icon(Icons.exit_to_app),
onTap: () async => await Auth.logoutUser().then(
(value) => Navigator.pushNamed(context, '/SignInPage')),
),
)
],
),
body: isUserAnAdmin() == true ? AdminPage() : PatientPage());
}
问题是我总是被路由到 PatientPage(),而不管 isAdmin 的值如何。我怀疑这是因为它没有等待 isUserAnAdmin() 返回的值。我以为我可以等待 isUserAnAdmin(),但我不能在 Scaffold 的主体中使用 await。
我认为我可以通过这样做来解决问题:
body: isUserAnAdmin().then((value) {
value == true ? AdminPage() : PatientPage();
})
但我收到一条错误消息:
The argument type 'Future<Null>' can't be assigned to the parameter type 'Widget'.
知道如何解决这个问题吗?
【问题讨论】:
标签: flutter google-cloud-firestore async-await