gpt4 book ai didi

flutter - Future Builder 不是在 build

转载 作者:行者123 更新时间:2023-12-05 02:35:26 26 4
gpt4 key购买 nike

我正在尝试使用分配给用户 firestore 文档中“isVerified”字段的 bool 值登录用户。

换句话说,如果'isVerified'为真则继续,否则返回验证页面。

我放入了 debugPrint 语句来帮助我捕获错误,并且 Future Builder 似乎没有通过构建器上下文。我已经阅读了有关 future build 者的其他文档,但我找不到哪里出错了,如果有什么我可以澄清的,请告诉我。谢谢

使用 Future Builder 实现异步

FutureBuilder (
future: getVerified(),
builder: (context, snapshot) { <--------- Nothing past this line is running
debugPrint('>> Home: FutureBuilder: checkpoint'); // does not print to console
if (snapshot.hasData && !snapshot.hasError) {
debugPrint('>> Home: FutureBuilder: Snapshot has data and no error');
}
return const Text('');
}
);

future

Future<bool> getVerified() async {
debugPrint('>> Home: getVerified Started');
User? user = auth.currentUser;
await FirebaseFirestore.instance
.collection('users')
.doc(user!.uid)
.get()
.then((value) {
bool isVerified = value.data()!['isVerified'];
debugPrint('>> Home: getVerified $isVerified'); // this variable is currently true or false
return isVerified; // this will return Instance of '_Future'
});
return false;
}

最佳答案

你不需要改变 FutureBuilder 这很好。我重新编写了您的 getVerified() 函数。

你能试试吗

Future<bool> getVerified() async {
debugPrint('>> Home: getVerified Started');
bool isVerified = false; // set your response to false

// get your user
final user = FirebaseAuth.instance.currentUser;

// check the data from firestore if the user is not null
if (user != null) {
final docSnapShot = await FirebaseFirestore.instance
.collection('users')
.doc(user.uid)
.get();

if (docSnapShot.exists) {
isVerified = docSnapShot.data()!['isVerified'];
}
}

debugPrint(
'>> Home: getVerified $isVerified'); // this variable is currently true or false
return isVerified; // this will return Instance of '_Future'
}

关于flutter - Future Builder 不是在 build ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70560865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com