gpt4 book ai didi

带有导航功能的 Flutter Provider

转载 作者:行者123 更新时间:2023-12-03 03:30:56 26 4
gpt4 key购买 nike

目前,我有一个具有 3 种状态响应(成功、错误、正在运行)的提供者和监听该提供者的消费者。当提供商的响应是成功时,是否可以推送到另一条路线?这是我的消费者:

class SplashScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: _splashWidget(context),
);
}

Widget _splashWidget(BuildContext context) {
return Consumer<AccessTokenProvider>(builder: (context, myModel, child) {
switch (myModel.accessToken.status) {
case Status.COMPLETED:
Navigator.of(context).pushReplacementNamed(MainRoute);
return null;

case Status.ERROR:
return ErrorScreen();

default:
return SplashProcessing();
}
}, );
}
}

这样做导航好吗?谢谢!

最佳答案

Widget _splashWidget(BuildContext context) {
return Consumer<AccessTokenProvider>(builder: (context, myModel, child) {
switch (myModel.accessToken.status) {
case Status.COMPLETED:
WidgetsBinding.instance.addPostFrameCallback((_) =>
Navigator.of(context).pushReplacementNamed(MainRoute)); // can also use: Future.microtask(() => Navigator.of(context). pushReplacementNamed... );
return Container(); // always return a widget in the build method, not null

case Status.ERROR:
return ErrorScreen();

default:
return SplashProcessing();
}
}, );
}

关于带有导航功能的 Flutter Provider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63955564/

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