如何在onGenerateRoute中使用提供商?

时间:2019-07-30 15:53:32

标签: flutter dart flutter-provider

如何在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,
      ),
    ),);

}
}

Router.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;
    }
  }
}

=== MY TRY ===

main.dart

onGenerateRoute: Router(context).generateRoute,

Router.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。”

2 个答案:

答案 0 :(得分:2)

在这种情况下,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)

答案 1 :(得分:-1)

我使用“客户”小部件来完成