anonymous-dev
anonymous-dev

Reputation: 3489

How can I implement a provider that is depended on another provider?

I am using the provider package with flutter and dart I have a authentication provider and a stream provider that provides a user model. The stream provider that provides the user model is depended on the authentication provider. How can I get my user id to my stream provider?

This is my multiprovider

MultiProvider(
      providers: [
        Provider<AuthenticationProvider>(builder: (context) => AuthenticationProvider(),),
        StreamProvider<UserModel>.value(value: userStream(//here we call the async method in the Authentication provider to get the user id),
        ),
      ]

This is the method inside the Authentication Provider returns a authentication model which contains the user id

  Future<UserAuthenticationCertificate> userAuthenticationCertificate() async
  {
    FirebaseUser authenticatedUser = await _authentication.currentUser();
    if(authenticatedUser != null)
      return UserAuthenticationCertificate.fromFirebaseAuthentication(authenticatedUser);
    return null;
  }

This is the authentication certificate

import 'package:firebase_auth/firebase_auth.dart';

class UserAuthenticationCertificate
{
  String _userID;

  String get userID{
    return _userID;
  }

  UserAuthenticationCertificate(this._userID);

  UserAuthenticationCertificate._internal(this._userID);

  factory UserAuthenticationCertificate.fromFirebase(FirebaseUser firebaseUser)
  {
    return UserAuthenticationCertificate._internal(
      firebaseUser.uid
    );
  }
}

Edit

This is what I currently have

Provider<AuthenticationProvider>(create: (_) => AuthenticationProvider(),),
Provider<UserProvider>(
  create: (_) => UserProvider(),
),
StreamProvider(create: (context) {
  return Provider.of<UserProvider>(context).userStream(Provider.of<AuthenticationProvider>(context).userAuthenticationCertificate());
}),

So now I provide the user provider, in a streamprovider? I want to be providing the userModel as the stream how do I do that?

Upvotes: 1

Views: 757

Answers (1)

R&#233;mi Rousselet
R&#233;mi Rousselet

Reputation: 276911

You can use the context parameter passed to create of your providers to read other providers:

Provider(create: (_) => Auth()),
StreamProvider(create: (context) {
  return Provider.of<Auth>(context).something;
});

Upvotes: 2

Related Questions