Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conditional provider like with Provider Package #50

Closed
logemann opened this issue Jul 15, 2020 · 2 comments
Closed

conditional provider like with Provider Package #50

logemann opened this issue Jul 15, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@logemann
Copy link

Is your feature request related to a problem? Please describe.

I cant find a way to add a provider based on the value of a stream (in this case, the value of a firebase auth).

Describe the solution you'd like

Take this from provider package. This widgets sits on top of MaterialApp and get a builder which in fact is the whole app. So its a way to "inject" providers (here a firestore db provider with a valid connection based on user credentials) after a successful login.

class AuthWidgetBuilder extends StatelessWidget {
  const AuthWidgetBuilder({Key key, @required this.builder}) : super(key: key);
  final Widget Function(BuildContext, AsyncSnapshot<User>) builder;

  @override
  Widget build(BuildContext context) {
    final authService = Provider.of<AuthService>(context, listen: false);
    return StreamBuilder<User>(
      stream: authService.onAuthStateChanged,
      builder: (BuildContext context, AsyncSnapshot<User> snapshot) {
        final User user = snapshot.data;
        if (user != null) {
          return MultiProvider(
            providers: <SingleChildCloneableWidget>[
              Provider<User>.value(value: user),
              ProxyProvider<User, FirestoreDatabase>(
                update: (_, User user, __) => FirestoreDatabase(uid: user.uid),
              )
            ],
            child: builder(context, snapshot),
          );
        }
        return builder(context, snapshot);
      },
    );
  }
}

Additional context

I think "emulating" ProxyProvider in Riverpod should be quite easy with the "ref" variable but the whole concept of conditional providers is not clear. Perhaps i am missing something very basic here.

@logemann logemann added the enhancement New feature or request label Jul 15, 2020
@rrousselGit
Copy link
Owner

rrousselGit commented Jul 15, 2020

You could do:

final auth = Provider((ref) => AuthService());

final user = StreamProvider((ref) async* {
  await for (final user in ref.read(auth).onAuthStateChanged) {
    yield user;
  }
});


final db = StreamProvider((ref) async* {
  await for (final user in ref.read(user)) {
    yield FirestoreDatabase(uid: user.uid);
  }
});

@logemann
Copy link
Author

This looks pretty nice. I assume that you can put those in global scope right? As with other providers...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants