gpt4 book ai didi

Flutter:在无状态小部件中调用异步方法

转载 作者:行者123 更新时间:2023-12-03 04:48:53 27 4
gpt4 key购买 nike

我有一个要在无状态小部件中调用的异步方法。运行时它说

Future dynamic Is not a sub type of widget

然后它会在几秒钟后显示结果。我该如何停止它?这是我的方法

    var onvalue = await Firestore.instance
.collection("userdata")
.document(user.uid)
.get();
if (onvalue.exists) {
return Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => mainhomepage()),);
}
else {
print("register");
return Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => Register()),);
}
}

还有我的主体

Widget build(BuildContext context) {
var user = Provider.of<User>(context);
//return either home or authenticate
return MaterialApp (
home: user != null?check(user)//Calling the function
:app()
);
}

最佳答案

您可以使用 Futurebuilder。

Widget build(BuildContext context) {
var user = Provider.of<User>(context);
//return either home or authenticate
return MaterialApp (
home: user != null?
FutureBuilder(
future: check(user),
builder: (_, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text(snapshot.data);
} else {
return CircularProgressIndicator();
}
},
)
:app()
);
}

关于Flutter:在无状态小部件中调用异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61750697/

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