0

Let's say I have 3 ChangeNotifierProviders "Auth1","Auth2","Auth3". I want all of them to implement login(),signup(),isAuthenticated().

So that I can replace one provider with another whenever I want.

//Interface
class IAuth {
 Future<void> signUp(String email, String password)=>throw UnimplementedError();
 Future<void> login(String email, String password) => throw UnimplementedError();
 Future<bool> isAuthenticted() => throw UnimplementedError();
 void logout() => throw UnimplementedError();
}

I can define

class Auth1 extends ChangeNotifier implements IAuth { ... }

class Auth2 extends ChangeNotifier implements IAuth { ... }

class Auth3 extends ChangeNotifier implements IAuth { ... }

But how can I provide the providers, in such a way that I can replace them without any problem in the future?

ChangeNotifierProvider.value(value: Auth1())

After sometime, I may want to replace Auth1 with Auth2 and also I want to know whether this is a trivial question

Ken White
  • 123,280
  • 14
  • 225
  • 444
Saravanan
  • 566
  • 5
  • 13
  • Why do you want to replace providers? – Abdur Rafay Saleem May 20 '21 at 21:39
  • Suppose let's say I have various authentication providers, one using JWT, another using some other authentication mechanism. I wanted my provider to work regardless of what authentication provider I use. – Saravanan May 21 '21 at 05:26
  • This seems like a similar problem I am facing here https://stackoverflow.com/questions/76403906/. Did you find any elegant solution? – itsji10dra Jun 08 '23 at 22:38

2 Answers2

0

This might be easier using ProviderScope in RiverPod. (RiverPod was written by the author of Provider to solve a new range of problems.)

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
0

I'm a little late to the party; However; I just faced the same issue and the solution is to create an abstract class that extends ChangeNotifier and then create Provider with your implementation of the abstract class.

abstract class AbstractAuthProvider extends ChangeNotifier {
    int? _userId;
    int get userId => userId
}
    
class Auth1Provider extends ChangeNotifier implements AbstractAuthProvider {
  int? _userId;
  int get userId => userId
}

class Auth2Provider extends ChangeNotifier implements AbstractAuthProvider {
  int? _userId;
  int get userId => userId
}

class Auth3Provider extends ChangeNotifier implements AbstractAuthProvider {
  int? _userId;
  int get userId => userId
}
    
Provider<AbstractAuthProvider>(
    create: (_) => Auth1Provider(),
    child: Consumer<AbstractAuthProvider>(context, auth1Provider, child){
        final userId = Provider.of<AbstractAuthProvider>(context).userId;
        return Column(
            childrens: [
                Text(userId.toInt()) /// will print user id value from auth1 provider
                Container(
                  child: Provider<AbstractAuthProvider>(
                    create: (_) => Auth2Provider(),
                    child: Consumer<AbstractAuthProvider>(context, auth2Provider, child){
                      final userId2 = Provider.of<AbstractAuthProvider>(context).userId;
                      return Text(userId2.toInt()) // will print user id value from auth 2 provider
                      // it will always get the value of nearest provider
                    }
                  )
                )
            ]
        )
    }
)
Mohammad Ahmad
  • 715
  • 8
  • 22