Search code examples
flutterriverpod

How can I access and adjust provider inside main() or runApp() to apply the value from firebase auth?


  FirebaseAuth.instance.authStateChanges().listen((User? user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      print('User is signed in!');
    }
  });

I have this function inside the main() {}. I want to change riverpod state when it detects changes to Firebase Auth. But, right now I am stuck because I can't access my riverpod state without using WidgetRef ref that uses widget.

If I put that into my main widget, then there is a bit of a delay. Or, is this not recommended to do - putting firebase.auth function inside the main()?


Solution

  • This function shouldn't be in your main func, what you can do is to modify your main.dart to look like this:

    void updateState(WidgetRef ref) {
    FirebaseAuth.instance.authStateChanges().listen((User? user) {
        if (user == null) {
          print('User is currently signed out!');
          ///update state with ref....
    
        } else {
          print('User is signed in!');
    
          ///update state with ref....
    
        }
      });
    }
        void main() {
          runApp(const ProviderScope(child: MyApp()));
        }
        
        class MyApp extends ConsumerWidget {
          const MyApp({super.key});
        
          // This widget is the root of your application.
          @override
          Widget build(BuildContext context, WidgetRef ref) {
        
            WidgetsBinding.instance.addPostFrameCallback((_) {
             updateState(ref);
            });
            return MaterialApp(
              title: 'Flutter Demo',
              theme: MyTheme.appTheme,
              debugShowCheckedModeBanner: false,
              home: const HomeScreen(),
            );
          }
        }
    

    Another approach could be to use an initstate in a ConsumerStatefulWidget, would look like:

      void updateState(WidgetRef ref) {
    FirebaseAuth.instance.authStateChanges().listen((User? user) {
        if (user == null) {
          print('User is currently signed out!');
          ///update state with ref....
    
        } else {
          print('User is signed in!');
    
          ///update state with ref....
    
        }
      });
    }
        void main() {
          runApp(const ProviderScope(child: MyApp()));
        }
        
        class MyApp extends ConsumerStatefulWidget {
          const MyApp({super.key});
        
          @override
          ConsumerState<MyApp> createState() => _MyAppState();
        }
        
        class _MyAppState extends ConsumerState<MyApp> {
          // This widget is the root of your application.
        
          @override
          void initState() {
            // TODO: implement initState
            updateState(ref);
            super.initState();
          }
        
          @override
          Widget build(BuildContext context) {
            return MaterialApp(
              title: 'Flutter Demo',
              theme: MyTheme.appTheme,
              debugShowCheckedModeBanner: false,
              home: const HomeScreen(),
            );
    
      }
    }