本文介绍了如何设置从另一个StreamProvider获取参数值的StreamProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用FirebaseAuth和Firestore.

I'm using FirebaseAuth and Firestore for my application.

用户"是一个模型,表示Firestore上的文档,并使用FirebaseUser中的uid为其文档ID.

'User' is a model which represent a document on firestore and use uid from FirebaseUser to be its DocumentId as well.

为了获得用户",我必须从FirebaseUser传递uid.

In order to get a 'User', I have to pass in uid from FirebaseUser.

我的想法是设置一个FirebaseUser的StreamProvider,然后设置另一个User的StreamProvider,它将FirebaseUser作为参数.

My idea is setup a StreamProvider of FirebaseUser, and then setup another StreamProvider of User, which takes FirebaseUser as a param.

但是发生错误,我找不到此问题的解决方案,任何建议对我都有很大帮助.

But error occurs, I couldn't found the solution for this issue, any advice would help me very much.

List<SingleChildCloneableWidget> uiConsumableProviders = [
  StreamProvider<FirebaseUser>(
    builder: (context) => Provider.of<LoginNotifier>(context, listen: false).streamFirebaseUser,
  ),
  StreamProvider<User>(
    builder: (context) =>
        Provider.of<UserNotifier>(context, listen: false).streamUser(Provider.of<FirebaseUser>(context)),
  ),
];
I/flutter (15813): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (15813): The following assertion was thrown building InheritedProvider<FirebaseUser>:
I/flutter (15813): inheritFromWidgetOfExactType(InheritedProvider<FirebaseUser>) or inheritFromElement() was called
I/flutter (15813): before BuilderStateDelegate<Stream<User>>.initDelegate() completed.
I/flutter (15813): 
I/flutter (15813): When an inherited widget changes, for example if the value of Theme.of()
I/flutter (15813): changes, its dependent widgets are rebuilt. If the dependent widget's reference
I/flutter (15813): to the inherited widget is in a constructor or an initDelegate() method, then
I/flutter (15813): the rebuilt dependent widget will not reflect the changes in the inherited
I/flutter (15813): widget.
I/flutter (15813): 
I/flutter (15813): Typically references to inherited widgets should occur in widget build()
I/flutter (15813): methods. Alternatively, initialization based on inherited widgets can be placed
I/flutter (15813): in the didChangeDependencies method, which is called after initDelegate and
I/flutter (15813): whenever the dependencies change thereafter.
I/flutter (15813):

推荐答案

一些解决方法之后,我通过将rxdart中的ObservableProvider结合起来解决了我的问题.像这样:

After some workarounds, I've solved my issue by combining Observable from rxdart with Provider. Like this:

Stream<User> get userStream {
return Observable(_firebaseAuth.onAuthStateChanged).switchMap((user) {
  if (user != null) {
    return userCollection
        .document(user.uid)
        .snapshots()
        .map((doc) => User.fromDocumentSnapshot(doc))
        .handleError(_catchError);
  } else {
    return Observable<User>.just(null);
  }
});}

外部MultiProvider:

Outside MultiProvider:

StreamProvider<User>(builder: (context) => Provider.of<FireStoreService>(context, listen: false).userStream,),

希望这将帮助面临相同问题的任何人.

Hope this will help anyone facing the same issue.

这篇关于如何设置从另一个StreamProvider获取参数值的StreamProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:25