English 中文(简体)
How to take advantages of interfaces while using Providers in flutter?
原标题:

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

问题回答

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

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
                    }
                  )
                )
            ]
        )
    }
)




相关问题
Share & Embed Widget design samples?

I am searching for some sample interface or design for creating a "Share & Embed Widget or Link" interface in my website. I found scribd.com s interface interesting http://www.scribd.com/doc/...

Practical use of interface events [closed]

What is a good example of the power of interface events (declaring events inside interface)? Most of the times I have seen only public abstract methods inside interface.

Linqtosql - Returning custom class with interface

Why can t I return as a new custom class (cms.bo.Site) which implements ISite? public IQueryable<ISite> GetSites() { return (from site in Db.Sites select new cms.bo.Site(site.id, site.name))...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签