gpt4 book ai didi

flutter - 如何在 onGenerateRoute 中使用 provider?

转载 作者:行者123 更新时间:2023-12-04 13:00:52 25 4
gpt4 key购买 nike

如何在 onGenerateRoute 中使用我的 UserProvider?
我想检查用户是否是使用 UserProvider.isLogged() 的记录器,但是当我尝试获取 Userprovider 时,我必须传递我在路由器中没有的上下文。

我已经尝试将 generateRoute 更改为非静态并通过构造函数将上下文传递给 Router 但它抛出错误。

main.dart

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {

return MultiProvider(
providers: <SingleChildCloneableWidget>[
ChangeNotifierProvider(builder: (_) => BottomNavigationBarProvider()),
ChangeNotifierProvider(builder: (_) => UserProvider()),
],
child: MaterialApp(
initialRoute: '/',
onGenerateRoute: Router.generateRoute,
title: 'Title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
),);

}
}

路由器.dart
class Router {

static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => HomePage());
break;
case '/login':
return MaterialPageRoute(builder: (_) => LoginPage());
break;
case '/register':
return MaterialPageRoute(builder: (_) => RegisterPage());
break;
case '/terms':
return MaterialPageRoute(builder: (_) => TermsPage());
break;
case '/home':
return MaterialPageRoute(builder: (_) {
return HomePage();
});
break;
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}')),
));
break;
}
}
}

===我的尝试===

main.dart
onGenerateRoute: Router(context).generateRoute,

路由器.dart
class Router {
Router(this.context);

BuildContext context;

Route<dynamic> generateRoute(RouteSettings settings) {
final UserProvider userProvider = Provider.of<UserProvider>(context);
userProvider.isLogged();
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => HomePage());
break;
case '/login':
return MaterialPageRoute(builder: (_) => LoginPage());
break;
case '/register':
return MaterialPageRoute(builder: (_) => RegisterPage());
break;
case '/terms':
return MaterialPageRoute(builder: (_) => TermsPage());
break;
case '/home':
return MaterialPageRoute(builder: (_) {

return HomePage();});
break;
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}')),
));
break;
}
}
}

这个“我的尝试”给了我错误。我怎样才能做到这一点? “在此 MyApp 小部件上方找不到正确的 ProviderProvider>。”

最佳答案

在这种情况下 context不包括提供者。所以可以通过将MaterailApp放入MultiProvider的builder方法中来实现,例如:

class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context1) {

return MultiProvider(
providers: <SingleChildCloneableWidget>[
ChangeNotifierProvider(builder: (_) => BottomNavigationBarProvider()),
ChangeNotifierProvider(builder: (_) => UserProvider()),
],
builder: (context2, _) => MaterialApp(
initialRoute: '/',
onGenerateRoute: Router(context2).generateRoute,
title: 'Title',
theme: ThemeData(
primarySwatch: Colors.blue,
),
),
);
}
}
现在 UserProvider可以从 context2 访问调用 Provider.of<UserProvider>(context2)

关于flutter - 如何在 onGenerateRoute 中使用 provider?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275594/

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