I am trying to prevent unauthenticated users from navigating to a route using a URL in a flutter web app. But I have problems with providers

Viewed 78

So I am using this answer Prevent unauthenticated users from navigating to a route using a URL in a flutter web app? to make my task.
But I always have this error: Error: Could not find the correct Provider<UserProvider> above this Consumer<UserProvider> Widget.
My main.dart:

import '/providers/user_provider.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final Future<FirebaseApp> _initialization = Firebase.initializeApp();
    return FutureBuilder(
      future: _initialization,
      builder: (context, appSnapshot) {
        return MaterialApp(
         //smth
          onUnknownRoute: (RouteSettings settings) {
            return MaterialPageRoute<void>(
              settings: settings,
              builder: (BuildContext context) => NotFoundErrorScreen(),
            );
          },
          routes: {
            AuthScreen.routeName: (context) => AuthScreen(),
            DashboardScreen.routeName: (context) => DashboardScreen(),
            ProductsScreen.routeName: (context) => ProductsScreen(),
            PermissionErrorScreen.routeName: (context) => PermissionErrorScreen(),
          },
          builder: (context, child) {
            return Consumer<UserProvider>(
              child: DashboardScreen(),
              builder: (context, provider, child) {
                if (provider.isLoading) {
                  return SplashScreen();
                }

                final value = provider.user;
                if (value == null) {
                  return Navigator(
                    onGenerateRoute: (settings) => MaterialPageRoute(
                        settings: settings, builder: (context) => AuthScreen()),
                  );
                }

                return MultiProvider(
                  providers: [
                    ChangeNotifierProvider(create: (context) => UserProvider()),
                  ],
                  child: child,
                );
              },
            );
          },
        );
      },
    );
  }
}

My user_provider.dart:

class UserProvider with ChangeNotifier {
  bool _isLoading = true;
  Map<String, dynamic> _user;

  bool get isLoading => _isLoading;

  Map<String, dynamic> get user => _user;

  getUser() async {
    FirebaseAuth.instance.authStateChanges().listen((currentUser) async {
      _isLoading = false;
      if (currentUser == null) {
        _user = null;
        notifyListeners();
        return;
      }
      final data = await FirebaseFirestore.instance
          .collection('users')
          .doc(currentUser.uid)
          .snapshots()
          .first;
      if (data.exists) {
        print(data.data());
        if (data.data()['role'] == 'admin') {
          _user = (data.data());
          notifyListeners();
        }
        return null;
      }
    });
  }
}

This is my first experience with flutter web, so I am confused. My first thaught was that it does not work, because I don't invoke my getUser() function, however I do not know how to fix it.

Thank you in advance.

0 Answers
Related