gpt4 book ai didi

flutter - 无法检查用户帐户是否存在?

转载 作者:行者123 更新时间:2023-12-03 03:19:39 25 4
gpt4 key购买 nike

Helo,我正在尝试检查用户的帐户是否存在,如果不存在,我要运行“帐户已删除”文本。
但是问题是,当我启动该应用程序时,屏幕上会显示现有帐户,只有重置后我才能获得真实的结果。
好像是在第一次运行应用程序后完成了帐户检查,但是我不知道哪里出了错。
这是代码,在此先感谢您:

class CheckIfDeletedAccount extends StatelessWidget {
String isAccountDeleted;

getData() async {
var userType = await Firestore.instance
.collection('users')
.where('userEmail', isEqualTo: email)
.getDocuments();
userType.documents.forEach((result) {
log(result.data["deleted"]);
isAccountDeleted = result.data["deleted"].toString();
});
}

@override
Widget build(BuildContext context) {
getData();
//log(isAccountDeleted);
if (isAccountDeleted == "true") {
return Scaffold(
body: Container(
child: Center(
child: Text("account is deleted"),
),
),
);
}
return MaterialApp(
theme: themeData,
home: Scaffold(
body: Bar(),
),
);
}
}

最佳答案

您需要等待Firebase的结果。您正在尝试在初始化isAccountDeleted之前构建窗口小部件。
在您的方案中,可以按以下方式使用FutureBuilder:

class CheckIfDeletedAccount extends StatelessWidget {
String isAccountDeleted;

Future<String> getData() async {
var userType = await Firestore.instance
.collection('users')
.where('userEmail', isEqualTo: email)
.getDocuments();
userType.documents.forEach((result) {
log(result.data["deleted"]);
isAccountDeleted = result.data["deleted"].toString();
});
return isAccountDeleted;
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if(snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
final isAccountDeleted = snapshot.data;

if (isAccountDeleted == "true") {
return Scaffold(
body: Container(
child: Center(
child: Text("account is deleted"),
),
),
);
}

return MaterialApp(
theme: themeData,
home: Scaffold(
body: Bar(),
),
);
}
return Center(child: const CircularProgressIndicator());
},
);
}
}

关于flutter - 无法检查用户帐户是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64500746/

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