0

I have two classes, AuthAPI and UserUpdate. both extends ChangeNotifier. The update() method under UserUpdate updates the the user.

I am trying to trigger the AuthAPI class when the Update class is done updating the user.

class AuthAPI extends ChangeNotifier {
   late String _currentUser;
   String get name => _currentUser;

}

class UserUpdate extends ChangeNotifier {
  void update(User currentUser) {
   try{
    // some api 
   } catch(e){
    // some error
   }
    notifyListeners(); 
  }
}

I tried to do this by using a condition that watches the Update class in the frontend and try to trigger AuthAPI afterwards. However, this did not work.

I am lost or what?

  • Riverpod can aggregate providers nicely, and the providerlistener interface is more powerful than the built-in listener interface. – Randal Schwartz Aug 21 '23 at 15:37

1 Answers1

1

You need to use ChangeNotifierProxyProvider. In your case it would be something like:

List<SingleChildWidget> providers;

providers = [
    ChangeNotifierProvider<AuthAPI>(create: (_) => AuthAPI()),
    ChangeNotifierProxyProvider<AuthAPI, UserUpdate>(
            create: (_) => UserUpdate(),
            update: (context, authAPI, previous) =>
                previous!
                  ..update(authApi))
];

runApp(MultiProvider(providers: providers, child: const MyApp()));

Andrija
  • 1,534
  • 3
  • 10
  • what logic am i suppose to add under update() method? – Lackson Munthali Aug 21 '23 at 14:27
  • Its okay. I am suppose to Do some custom work based on authApi that may call `notifyListeners`. – Lackson Munthali Aug 21 '23 at 14:32
  • Similar to what you did in your code - only you won't receive User object, but AuthApi object. Then do whatever you need - and if required, call notifyListeners() at the end. The framework will simply pass the new instance of the AuthApi object, and then it's up to you to do whatever you need. – Andrija Aug 22 '23 at 05:03