我应该如何在我的Flatter应用程序中使用FirebaseAuth.instance.onAuthStateChanged?
在我的Flatter应用程序中,我试图通过使用提供者包中的ChangeNotifier将Firebase用户对象传递给下行小部件。我的第一个想法是在StreamBuilder中这样做:我应该如何在我的Flatter应用程序中使用FirebaseAuth.instance.onAuthStateChanged?,firebase,flutter,dart,firebase-authentication,flutter-provider,Firebase,Flutter,Dart,Firebase Authentication,Flutter Provider,在我的Flatter应用程序中,我试图通过使用提供者包中的ChangeNotifier将Firebase用户对象传递给下行小部件。我的第一个想法是在StreamBuilder中这样做: Widget _authBuilder(context, Widget body) { final authModel = Provider.of<FAAuthModel>(context); return StreamBuilder( stream: Fireba
Widget _authBuilder(context, Widget body) {
final authModel = Provider.of<FAAuthModel>(context);
return StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (context, snapshot) {
authModel.user = snapshot.data;
return body;
},
);
}
Widget\u authBuilder(上下文,Widget主体){
最终authModel=Provider.of(上下文);
返回流生成器(
流:FirebaseAuth.instance.onAuthStateChanged,
生成器:(上下文,快照){
authModel.user=snapshot.data;
返回体;
},
);
}
唯一的问题是,显然构建器函数在构建body小部件时异步运行,因此状态同时被标记为脏。有没有更智能的方法将FirebaseUser传递到下行小部件树
I/flutter (28504): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
I/flutter (28504): The following assertion was thrown while dispatching notifications for FAAuthModel:
I/flutter (28504): setState() or markNeedsBuild() called during build.
I/flutter (28504): This _DefaultInheritedProviderScope<FAAuthModel> widget cannot be marked as needing to build because
I/flutter (28504): the framework is already in the process of building widgets. A widget can be marked as needing to
I/flutter (28504): be built during the build phase only if one of its ancestors is currently building. This exception
I/flutter (28504): is allowed because the framework builds parent widgets before children, which means a dirty
I/flutter (28504): descendant will always be built. Otherwise, the framework might not visit this widget during this
I/flutter (28504): build phase.
I/flutter (28504): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (28504): _DefaultInheritedProviderScope<FAAuthModel>
I/flutter (28504): The widget which was currently being built when the offending call was made was:
I/flutter (28504): StreamBuilder<FirebaseUser>
I/flutter (28504):
I/颤振(28504):══╡ 基金会例外╞════════════════════════════════════════════════════════
I/flatter(28504):在为FAAuthModel发送通知时引发了以下断言:
I/flatter(28504):在生成过程中调用setState()或markNeedsBuild()。
I/flatter(28504):无法将此_DefaultInheritedProviderScope小部件标记为需要生成,因为
I/flatter(28504):框架已经在构建小部件。小部件可以标记为需要
I/Flatter(28504):仅当其一个祖先当前正在构建时,才能在构建阶段构建。这个例外
I/flatter(28504):是允许的,因为框架在子窗口之前构建父窗口小部件,这意味着脏窗口
I/颤振(28504):将始终构建后代。否则,框架在此期间可能不会访问此小部件
I/颤振(28504):构建阶段。
I/flatter(28504):调用setState()或markNeedsBuild()的小部件是:
I/颤振(28504):\u默认继承的ProviderScope
I/flatter(28504):当发出违规呼叫时,当前正在构建的小部件是:
I/颤振(28504):StreamBuilder
I/颤振(28504):
所以这个问题的答案是,您不应该使用StreamBuilder,而应该使用侦听器
Widget _authBuilder(context, Widget body) {
Stream<FirebaseUser> stream;
final authModel = Provider.of<FAAuthModel>(context);
stream = FirebaseAuth.instance.onAuthStateChanged;
stream.listen((snapshot) {
/* We are not logged in */
if (snapshot == null && authModel.user == null) {
//Do nothing
}
/* We are not logged in, or we are logging out */
else if (snapshot == null) {
authModel.user = null;
/* We are logged in but we just opened our app */
} else if (authModel.user == null) {
authModel.user = snapshot;
/* We are logged in but something happened e.g. the widget tree was rebuilt and we just logged out? */
} else if (authModel.user.uid != snapshot.uid) {
authModel.user = snapshot;
}
});
return body;
}
Widget\u authBuilder(上下文,Widget主体){
溪流;
最终authModel=Provider.of(上下文);
stream=FirebaseAuth.instance.onAuthStateChanged;
stream.listen((快照){
/*我们没有登录*/
if(snapshot==null&&authModel.user==null){
//无所事事
}
/*我们没有登录,或者我们正在注销*/
else if(快照==null){
authModel.user=null;
/*我们已登录,但我们刚刚打开应用程序*/
}else if(authModel.user==null){
authModel.user=快照;
/*我们已登录,但发生了一些事情,例如,小部件树已重建,而我们刚刚注销*/
}else if(authModel.user.uid!=snapshot.uid){
authModel.user=快照;
}
});
返回体;
}
使用StreamBuilder和listener有什么区别吗?