skip to Main Content

i am building an app with firebaseauth and firebasestore. On the startup I am storing a constant value of the current user. From the uid it takes the id and gets the object out of the firebasestore database for more user data.

CustomUserData.withData({
required this.uid,
required this.firstname,
required this.lastname,
required this.birthday,
required this.shortname,
required this.rawData,
required this.isPassive,
required this.lastLogin,
required this.role,
required this.buildNumber,

This my CustomuserData class.

Now I want to store this object as a provider from riverpod. But I don’t know how to set this value on the startup to listens in the whole app after ist.
Currently I am always accessing my static variable.

Do you know any working solution ?

2

Answers


  1. You can do that. Simply wrap your MaterialApp or whatever your top level widget is with the provider.

    class DBViewer extends StatelessWidget {
      const DBViewer({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (context) => ProController()),
            ChangeNotifierProvider(create: (context) => TableController()),
          ],
          builder: (context, _) {
            Provider.of<ProController>(context, listen: false).init();
            return AnimatedBuilder(
              animation: dbService.darkModeListenable,
              builder: (context, child) {
                return MaterialApp(
    

    My app is using providers from provider package, but should be same for riverpod as well.

    Login or Signup to reply.
  2. you can try it so:

    final myReceivedDataProvider = FutureProvider<CustomUserData>((ref) async {
      return CustomUserData.withData(...);
    });
    
    class MyPage extends ConsumerWidget {
      const MyPage({
        Key? key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context, WidgetRef ref) {
        final data = ref.watch(myReceivedDataProvider);
        
        return data.when(
          data: (CustomUserData data) {
            // TODO
            return Text('$data');
          },
          error: (e, s) {
            return Text('$e, $s');
          },
          loading: () => CircularProgressIndicator(),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search