【发布时间】:2026-01-30 17:30:01
【问题描述】:
我的代码中有这部分我想在整个项目中重复,我希望能够在其他地方调用这个函数,但问题是你需要将snapshot.data 传递给widget树,有什么想法吗?
Widget UserStreamBuilderShortcut(BuildContext context, Widget widget){
final _user = Provider.of<UserModel>(context);
return
StreamBuilder<UserModel>(
stream: UserProvider(userID: _user.userID).userData,
builder: (context, snapshot){
if(snapshot.hasData == false){
return LoadingFullScreenLayer();
} else {
UserModel userModel = snapshot.data; // cant pass this to instances of UserStreamBuilderShortcut Widget
return
widget; // in instances of this UserStreamBuilderShortcut we need to be able to pass snapshot.data here
}
},
);
}
【问题讨论】:
标签: flutter dart stream-builder