如何使用 flutter_bloc 包将现有存储库实例传递到 Flutter 中的下一页?
How to pass an existing repository instance to the next page in Flutter using the flutter_bloc package?
我有以下代码将 bloc 传递到下一页
Navigator.of(context).push(
MaterialPageRoute<CreateFeedSelectClass>(
builder: (_) => BlocProvider.value(
value: BlocProvider.of<TeacheractivityfeedBloc>(context),
child: CreateFeedSelectClass(imageStringList: state.selectedImages, imagePathList: state.selectedImagePaths, classList: state.classList,))
)
);
我想传递在构建函数之前已经在此页面中创建的这个存储库
TeacherRepository _teacherRepository = TeacherRepository();
来自 flutter bloc 的示例 (https://pub.dev/packages/flutter_bloc)
只有一种创建新实例的方法,我想传递现有实例而不是创建新实例:
RepositoryProvider(
create: (context) => RepositoryA(), //----->
child: ChildA(),
);
而不是
创建:有什么方法可以像 value: _teacherRepository 那样做,就像我们用 BlocProvider.value(value:....) 做的那样?
实际上有一个命名构造函数 value
可用于 RepositoryProvider
,您可以使用它。
RepositoryProvider.value(
value: repository,
child: Container(),
);
我有以下代码将 bloc 传递到下一页
Navigator.of(context).push(
MaterialPageRoute<CreateFeedSelectClass>(
builder: (_) => BlocProvider.value(
value: BlocProvider.of<TeacheractivityfeedBloc>(context),
child: CreateFeedSelectClass(imageStringList: state.selectedImages, imagePathList: state.selectedImagePaths, classList: state.classList,))
)
);
我想传递在构建函数之前已经在此页面中创建的这个存储库
TeacherRepository _teacherRepository = TeacherRepository();
来自 flutter bloc 的示例 (https://pub.dev/packages/flutter_bloc) 只有一种创建新实例的方法,我想传递现有实例而不是创建新实例:
RepositoryProvider(
create: (context) => RepositoryA(), //----->
child: ChildA(),
);
而不是 创建:有什么方法可以像 value: _teacherRepository 那样做,就像我们用 BlocProvider.value(value:....) 做的那样?
实际上有一个命名构造函数 value
可用于 RepositoryProvider
,您可以使用它。
RepositoryProvider.value(
value: repository,
child: Container(),
);