4

I just started with Provider package of flutter, I learnt that we have to provide the provider in a parent class and that is what is did. But I am still getting this error. I have a feeling it has something to do with the route generator. Any help is much appreciated

My main app code:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  final MaterialColor primary =
      MaterialColor(0xff1A365D, <int, Color>{500: Color(0xff1A365D)});
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => AuthInfoProvider(),
      child: MaterialApp(
        title: 'xxx',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            // This is the theme of your application.
            //
            // Try running your application with "flutter run". You'll see the
            // application has a blue toolbar. Then, without quitting the app, try
            // changing the primarySwatch below to Colors.green and then invoke
            // "hot reload" (press "r" in the console where you ran "flutter run",
            // or simply save your changes to "hot reload" in a Flutter IDE).
            // Notice that the counter didn't reset back to zero; the application
            // is not restarted.
            primaryColor: Color(0xff1A365D),
            accentColor: Color(0xff4299E1),
            buttonColor: Color(0xff1A365D),
            floatingActionButtonTheme: FloatingActionButtonThemeData(
                backgroundColor: Color(0xff1A365D))),
        onGenerateRoute: RouteGenerator.generateRoute,
        initialRoute: '/auth/wrapper',
      ),
    );
  }
}

My Route generator code:

class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final args = settings.arguments;

    switch (settings.name) {
      case '/auth/wrapper':
        return MaterialPageRoute(builder: (_) => AuthWrapper());
      case '/auth/wrapper/login':
        return MaterialPageRoute(builder: (_) => Login());
      case '/app/activeevents':
        return MaterialPageRoute(builder: (_) => PaginatedActiveEvents());
      default:
        return MaterialPageRoute(builder: (_) => ErrorRoute());
    }
  }
}

My App Wrapper Code where I need to use the provider.

class AuthWrapper extends StatelessWidget {
  const AuthWrapper({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final authInfo = Provider.of<AuthInfo>(context);
    return authInfo.isFirst ? Landing() : Signup();
  }
}

My Provider code :

class AuthInfoProvider with ChangeNotifier {
  AuthInfo _authInfo = new AuthInfo();

  AuthInfo get authInfo => _authInfo;

  void setIsFirst() {
    _authInfo.isFirst = false;
    notifyListeners();
  }
}

The Error I am getting: enter image description here

I am new to Provider, how do I fix this. Help is very much appreciated

Sayanc2000
  • 212
  • 2
  • 6

0 Answers0