如何创建 StreamBuilder<UserModel> 实例
how to create instances of StreamBuilder<UserModel>
我的代码中有这部分我想在整个项目中重复,我希望能够在其他地方调用这个函数,但问题是你需要将 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
}
},
);
}
您可以为此指定构建器参数
// define a Function type that takes a BuildContext and UserModel
// and returns a Widget
typedef UserModelWidgetBuilder = Widget Function(
BuildContext context,
UserModel userModel,
);
// pass it as a parameter in the shortcut function
Widget UserStreamBuilderShortcut({
BuildContext context,
UserModelWidgetBuilder builder,
}) {
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;
return builder(context, userModel);
}
},
);
}
现在可以这样调用了:
UserStreamBuilderShortcut(
context: context,
builder: (context, UserModel userModel) {
return Container(); // create widget with UserModel
},
),
如果您使用 Provider 包,您可以从它的子窗口小部件调用它的流参数。
父控件:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<BrewList>.value(
value: Database().brewList,
child: MaterialApp(
home: BrewListTile(),
),
);
}
}
有权访问流的子微件。
class _BrewListState extends State<BrewList> {
@override
Widget build(BuildContext context) {
final brews = Provider.of<List<Brew>>(context) ?? [];
return Text(brews[0].text);
我的代码中有这部分我想在整个项目中重复,我希望能够在其他地方调用这个函数,但问题是你需要将 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
}
},
);
}
您可以为此指定构建器参数
// define a Function type that takes a BuildContext and UserModel
// and returns a Widget
typedef UserModelWidgetBuilder = Widget Function(
BuildContext context,
UserModel userModel,
);
// pass it as a parameter in the shortcut function
Widget UserStreamBuilderShortcut({
BuildContext context,
UserModelWidgetBuilder builder,
}) {
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;
return builder(context, userModel);
}
},
);
}
现在可以这样调用了:
UserStreamBuilderShortcut(
context: context,
builder: (context, UserModel userModel) {
return Container(); // create widget with UserModel
},
),
如果您使用 Provider 包,您可以从它的子窗口小部件调用它的流参数。
父控件:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<BrewList>.value(
value: Database().brewList,
child: MaterialApp(
home: BrewListTile(),
),
);
}
}
有权访问流的子微件。
class _BrewListState extends State<BrewList> {
@override
Widget build(BuildContext context) {
final brews = Provider.of<List<Brew>>(context) ?? [];
return Text(brews[0].text);