提供集团的正确方法是什么?
what is the right way to provide a bloc?
大家好,我是 flutter 的新手,我的问题是提供 bloc 的正确方法是什么?
我决定在名为 rout 部分的主文件中提供主题,但问题是当我需要一个位于其他 bloc 内部的 bloc 时,我无法提供它。
这是我的主要文件路由部分问题是例如我想向 assetPage bloc 提供一个 AssetDevicesPageBloc bloc 我怎么能做这样的事情因为我没有访问权限。我是否以错误的方式提供集团?我第一次想在一个地方提供所有 bloc,然后对每个小部件使用 blockprovider.value 但是当我搜索它时,我发现它使用 resources.please help.thank you
return MaterialApp(
routes: {
'loginPage': (context) => BlocProvider(
child: LogInPage(),
create: (BuildContext context) {
return LoginBloc(
userRepository: userRepository,
authenticationBloc:
BlocProvider.of<AuthenticationBloc>(context));
},
),
'AssetDevicePage': (context) => MultiBlocProvider(
child: AssetDevicesPage(),
providers: [
BlocProvider(
create: (BuildContext context) => AssetDevicesPageBloc(
userRepository: userRepository,
dataBaseRepository: dataBaseRepository,
deviceRepository: deviceRepository)),
BlocProvider(
create: (BuildContext context) => DeviceViewSwitcherBloc(
dataBaseRepository, userRepository, deviceRepository)),
BlocProvider(
create: (BuildContext context) =>
LampBloc(deviceRepository, dataBaseRepository)),
],
),
'AssetPage': (context) => MultiBlocProvider(
providers: [
BlocProvider(
create: (BuildContext context) =>
CategoryBloc(userRepository, dataBaseRepository),
),
BlocProvider(
create: (BuildContext context) =>
AssetPageBloc(BlocProvider.of<CategoryBloc>(context)),
),
],
child: AssetPage(),
),
},
出现此问题是因为该 bloc 在您调用它的上下文中不可用。
您需要在 materialApp 小部件上方的第一个小部件中初始化 AssetDevicesPageBloc。
例如在 MyApp Widget 中创建 AssetDevicesPageBloc 的实例。
AssetDevicesPageBloc assetDevicePageBloc = AssetDevicesPageBloc(
userRepository: userRepository,
dataBaseRepository: dataBaseRepository,
deviceRepository: deviceRepository)
);
然后用 BlocProvider.value 包装 MaterialApp 小部件并像这样提供创建的块:
BlocProvider.value(
value: assetDevicePageBloc,
child: MaterialApp(),
);
现在您可以使用以下方式在任何屏幕内的任何位置访问您的块:
BlocProvider.of< AssetDevicesPageBloc >(context)
但是请记住,如果您初始化了 bloc,您应该在初始化它的小部件的 dispose 方法中关闭它的实例。所以打电话
assetDevicePageBloc?.close();
在您初始化 assetDevicePageBloc 的小部件的 dispose 方法中。
大家好,我是 flutter 的新手,我的问题是提供 bloc 的正确方法是什么? 我决定在名为 rout 部分的主文件中提供主题,但问题是当我需要一个位于其他 bloc 内部的 bloc 时,我无法提供它。 这是我的主要文件路由部分问题是例如我想向 assetPage bloc 提供一个 AssetDevicesPageBloc bloc 我怎么能做这样的事情因为我没有访问权限。我是否以错误的方式提供集团?我第一次想在一个地方提供所有 bloc,然后对每个小部件使用 blockprovider.value 但是当我搜索它时,我发现它使用 resources.please help.thank you
return MaterialApp(
routes: {
'loginPage': (context) => BlocProvider(
child: LogInPage(),
create: (BuildContext context) {
return LoginBloc(
userRepository: userRepository,
authenticationBloc:
BlocProvider.of<AuthenticationBloc>(context));
},
),
'AssetDevicePage': (context) => MultiBlocProvider(
child: AssetDevicesPage(),
providers: [
BlocProvider(
create: (BuildContext context) => AssetDevicesPageBloc(
userRepository: userRepository,
dataBaseRepository: dataBaseRepository,
deviceRepository: deviceRepository)),
BlocProvider(
create: (BuildContext context) => DeviceViewSwitcherBloc(
dataBaseRepository, userRepository, deviceRepository)),
BlocProvider(
create: (BuildContext context) =>
LampBloc(deviceRepository, dataBaseRepository)),
],
),
'AssetPage': (context) => MultiBlocProvider(
providers: [
BlocProvider(
create: (BuildContext context) =>
CategoryBloc(userRepository, dataBaseRepository),
),
BlocProvider(
create: (BuildContext context) =>
AssetPageBloc(BlocProvider.of<CategoryBloc>(context)),
),
],
child: AssetPage(),
),
},
出现此问题是因为该 bloc 在您调用它的上下文中不可用。
您需要在 materialApp 小部件上方的第一个小部件中初始化 AssetDevicesPageBloc。
例如在 MyApp Widget 中创建 AssetDevicesPageBloc 的实例。
AssetDevicesPageBloc assetDevicePageBloc = AssetDevicesPageBloc(
userRepository: userRepository,
dataBaseRepository: dataBaseRepository,
deviceRepository: deviceRepository)
);
然后用 BlocProvider.value 包装 MaterialApp 小部件并像这样提供创建的块:
BlocProvider.value(
value: assetDevicePageBloc,
child: MaterialApp(),
);
现在您可以使用以下方式在任何屏幕内的任何位置访问您的块:
BlocProvider.of< AssetDevicesPageBloc >(context)
但是请记住,如果您初始化了 bloc,您应该在初始化它的小部件的 dispose 方法中关闭它的实例。所以打电话
assetDevicePageBloc?.close();
在您初始化 assetDevicePageBloc 的小部件的 dispose 方法中。