问题描述
大家好,我正在制作一个带有波动的应用程序,对于普通用户管理员和未经身份验证的用户,它们具有不同的视图,但是我要执行的逻辑不起作用,我也不知道为什么.
Hello everyone I'm making an application with flutter which has different views for normal users admins and unauthenticated users, but the logic that I'm going for isn't working and I don't know why.
此处的PS仅是要区分管理员和普通用户.
P.S here I'm only trying to differentiate between admins and normal users.
这就是我想要做的:
Future<bool> isAdmin() async {
final currentUserUid = _firebaseAuth.currentUser.uid;
//What I'm trying to do here is get my isAdmin field which is created when a user is created
final DocumentSnapshot db = await
databaseReference.collection('users').doc(currentUserUid).get();
return db.data().containsKey('isAdmin');
}
这就是我试图实现的方式
This is how I'm trying to implement it
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../screens/auth/admin/admin_tab_bar_screen.dart';
import '../../screens/auth/user_tab_bar_screen.dart';
import '../../providers/auth.dart';
class AuthTabBarScreen extends StatelessWidget {
static const routeName = 'auth-tab-bar-view';
@override
Widget build(BuildContext context) {
final checkUserRole = Provider.of<Auth>(context, listen: false).isAdmin();
// Here I want to check the Value I'm returning if its true
if(checkUserRole == true){
return AdminTabBarScreen();
} else {
return UserTabBarScreen();
}
}
}
推荐答案
您可以使用 FutureBuilder
来等待将来的构建(在您的情况下为 isAdmin()
),然后再进行构建. snapshot.data
是 future
的值.
You can use FutureBuilder
which waits for a future (isAdmin()
in your case) before building. snapshot.data
is the value of the future
.
class AuthTabBarScreen extends StatelessWidget {
static const routeName = 'auth-tab-bar-view';
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Provider.of<Auth>(context, listen: false).isAdmin(),
builder: (context, snapshot) => snapshot.hasData
? snapshot.data // if isAdmin() is true
? AdminTabBarScreen() // return Admin
: UserTabBarScreen() // else if false, return UserTab
: Loading(), // while you're waiting for the data, show some kind of loading indicator
);
}
}
isAdmin()
返回作为未来值的 Future< bool>
,因此当您比较 Future< bool> 和 bool时
,您将收到一个相等错误,因为一个是 Future
,另一个是 bool
.使用 FutureBuilder()
,您的应用将等待 Provider.of< Auth> ... isAdmin()
返回值或 snapshot
. snapshot
是您未来的异步值,因此调用 snapshot.data
与 await Provider.of< Auth> ... isAdmin()相同.code>,这就是为什么
snapshot.data
返回 bool
而不是 Future< bool>
的原因.了解文档.
isAdmin()
returns Future<bool>
which is a future value so when you compare Future<bool>
and bool
, you will receive an equality error because one is a Future
and the other is a bool
. Using FutureBuilder()
, your app waits for Provider.of<Auth>...isAdmin()
to return a value or snapshot
. A snapshot
is an async value of your future so calling snapshot.data
is the same as await Provider.of<Auth>...isAdmin()
which is why snapshot.data
returns a bool
and not a Future<bool>
. It might be helpful to understand how futures, async, await work documentation.
这篇关于如何在FireStore中获取isAdmin字段并在Flutter中检查它是对还是错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!