Firebase 使用copywith时未更新颤振块状态
大纲:Firebase 使用copywith时未更新颤振块状态,firebase,flutter,google-cloud-firestore,bloc,Firebase,Flutter,Google Cloud Firestore,Bloc,大纲: 导航到配置文件页面 传入用户id以从firestore获取文档 将检索到的数据传递给state.copyWith(数据:data) 生成成功状态和当前用户界面 我的问题是,当我使用state.copyWith(data:data)时,状态没有被更新,即使数据100%存在,因为我可以在控制台中打印它 代码: UI: 类ProfileView扩展了无状态小部件{ 最后一个字符串uid; 最终UserRepository UserRepository=UserRepository(); Pro
UI:
类ProfileView扩展了无状态小部件{
最后一个字符串uid;
最终UserRepository UserRepository=UserRepository();
ProfileView({required this.uid});
@凌驾
小部件构建(构建上下文){
//初始化存储库
返回BlocProvider(
创建:(上下文)=>ProfileBloc(
//应该能够从上下文中提取用户repo
userRepository:userRepository(),
isCurrentUser:UserRepository().isCurrentUser(uid))
…添加(初始化配置文件(uid:uid)),
//获取用户文档
子项:_profileView(上下文));
}
小部件_profileView(上下文){
返回BlocListener(
侦听器:(上下文、状态){
if(state.imageSourceActionSheetIsVisible){
_showImageSourceActionSheet(上下文);
}
最终加载状态=state.loadingStatus;
如果(加载状态为加载失败){
showSnackBar(context,state.loadingStatus.exception.toString());
}
如果(加载状态为加载进度){
加载视图();
}
如果(加载状态为加载失败){
LoadingFailedView(异常:loadingStatus.exception.toString());
}
},
孩子:脚手架(
appBar:_appBar(),
正文:_profilePage(),
bottomNavigationBar:bottomNavigationBar(上下文),
),
);
}
集团:
类ProfileBloc扩展了Bloc{
最终目标是当前用户;
最终用户存储库用户存储库;
最终_imagePicker=imagePicker();
轮廓集团({
需要此.isCurrentUser,
需要此.userRepository,
}):super(ProfileState)(
isCurrentUser:isCurrentUser,loadingStatus:loadingprogress());
@凌驾
流映射事件状态(
事件简介,
)异步*{
if(事件为InitializeProfile){
yield*\u initProfile(uid:event.uid);
}
}
流_initProfile({必需字符串uid})异步*{
//加载视图
屈服状态.copyWith(loadingStatus:LoadingProgress());
//获取配置文件数据
试一试{
最终快照=等待userRepository.getUserDoc(uid);
if(snapshot.exists){
最终数据=snapshot.data();
打印(数据['fullName']);
屈服状态.copyWith(
fullName:data[“fullName”].toString(),
);
打印(state.fullName);
屈服状态.copyWith(loadingStatus:LoadingSuccess());
}
否则{
屈服状态.copyWith(
loadingStatus:LoadingFailed(“配置文件数据不存在:(”);
}
}捕获(e){
印刷品(e);
}
}
声明:
“侧面图”的一部分;
类配置文件状态{
最终目标是当前用户;
最后一个字符串?全名;
最终荷载状态荷载状态;
bool ImageSourceActionSheet可见;
轮廓状态({
必需的bool是当前用户,
这个,全名,
this.loadingStatus=const InitialLoadingStatus(),
imageSourceActionSheetIsVisible=false,
}):this.isCurrentUser=isCurrentUser,
this.imageSourceActionSheetIsVisible=imageSourceActionSheetIsVisible;
配置文件状态copyWith({
布尔?是当前用户,
字符串?全名,
装载状态?装载状态,
bool?ImageSourceActionSheet可见,
}) {
打印('State$fullName');
打印('State${this.fullName}');
返回配置文件状态(
isCurrentUser:this.isCurrentUser,
全名:全名??this.fullName,
loadingStatus:loadingStatus??this.loadingStatus,
imageSourceActionSheetIsVisible:imageSourceActionSheetIsVisible??
此.ImageSourceActionSheet可见,
);
}
}
新更新的代码实现了Bloc builder,而不是所建议的侦听器
Widget _profileView(context) {
return BlocBuilder<ProfileBloc, ProfileState>(builder: (context, state) {
final loadingStatus = state.loadingStatus;
if (loadingStatus is LoadingInProgress) {
return LoadingView();
}
if (loadingStatus is LoadingFailed) {
return LoadingFailedView(exception: loadingStatus.exception.toString());
}
if (loadingStatus is LoadingSuccess) {
return Scaffold(
appBar: _appBar(),
body: _profilePage(),
bottomNavigationBar: bottomNavbar(context),
);
}
return LoadingView();
});
}
Widget\u profileView(上下文){
返回BlocBuilder(生成器:(上下文,状态){
最终加载状态=state.loadingStatus;
如果(加载状态为加载进度){
返回LoadingView();
}
如果(加载状态为加载失败){
返回LoadingFailedView(异常:loadingStatus.exception.toString());
}
如果(loadingStatus为LoadingSuccess){
返回脚手架(
appBar:_appBar(),
正文:_profilePage(),
bottomNavigationBar:bottomNavigationBar(上下文),
);
}
返回LoadingView();
});
}
这段代码仍然停留在加载屏幕上,当我打印出各种状态时,它被注册为一个事件,但是在使用copy with之后,状态打印出null
谢谢你的帮助看起来你把这段代码中的概念搞乱了。 您的集团逻辑很好,问题在于UI层 我的意见:
- 代码> Buffistister-<代码>不更新您的UI(它不是为它设计的,而是用于单镜头操作,例如导航/对话框显示)。请考虑使用<代码> BlocBuilder < /C> > .<
类MyWidget扩展了StatefulWidget{
...
}
类MyWidgetState扩展了状态{
Widget _currentWidget;//在视图层次结构上使用它,记住使用默认Widget初始化!
...
@凌驾
小部件构建(构建上下文){
//初始化存储库
返回BlocProvider(
创建:(上下文)=>ProfileBloc(
//应该能够从上下文中提取用户repo
userRepository:userRepository(),
isCurrentUser:UserRepository().isCurrentUser(uid))
Widget _profileView(context) {
return BlocBuilder<ProfileBloc, ProfileState>(builder: (context, state) {
final loadingStatus = state.loadingStatus;
if (loadingStatus is LoadingInProgress) {
return LoadingView();
}
if (loadingStatus is LoadingFailed) {
return LoadingFailedView(exception: loadingStatus.exception.toString());
}
if (loadingStatus is LoadingSuccess) {
return Scaffold(
appBar: _appBar(),
body: _profilePage(),
bottomNavigationBar: bottomNavbar(context),
);
}
return LoadingView();
});
}