无论如何,我可以使用 MultiBlocProvider 来清理这个小部件巢吗?
Is there anyway that I can use MultiBlocProvider to clean this nest of widgets up?
我正在尝试清理这些乱七八糟的小部件,但我找不到这样做的方法。我的 NavigationBloc
依赖于 AuthenticationBloc
提供的流,为了防止内存泄漏,我必须关闭流。
需要 Builder 小部件,以便我可以获得 BlocProvider
提供的最新 BuildContext
,但我知道 MultiBlocProvider
会极大地清理它。我想避免将此小部件包装在 runApp
函数中,但我猜这是一个选项。
class _MyAppState extends State<MyApp> {
final authRepo = AuthRepo();
AuthenticationBloc authBloc;
@override
void dispose() {
authBloc?.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlocProvider<AuthenticationBloc>(
create: (_) =>
AuthenticationBloc(authRepo: authRepo)..add(InitializeAuth()),
child: Builder(builder: (context) {
authBloc = context.bloc<AuthenticationBloc>();
return BlocProvider<NavigationBloc>(
create: (_) => NavigationBloc(authBloc),
child: MaterialApp(
title: 'Arrow Manager',
debugShowCheckedModeBanner: false,
theme: appTheme(),
builder:
ExtendedNavigator<Router>(router: Router(), initialRoute: '/'),
),
);
}),
);
}
}
正如您所说,您可以使用 MultiProvider
来避免嵌套提供程序
您必须在 initState()
方法中创建 AuthenticationBloc
class _MyAppState extends State<MyApp> {
final authRepo = AuthRepo();
AuthenticationBloc authBloc;
@override
void initState() {
super.initState();
authBloc = AuthenticationBloc(authRepo: authRepo);
}
@override
void dispose() {
authBloc?.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (_) => authBloc..add(InitializeAuth()),
),
BlocProvider(
create: (context) => NavigationBloc(authBloc),
),
],
child: Builder(
builder: (context) {
authBloc = context.bloc<AuthenticationBloc>();
return MaterialApp(
title: 'Arrow Manager',
debugShowCheckedModeBanner: false,
theme: appTheme(),
builder: ExtendedNavigator<Router>(router: Router(), initialRoute: '/'),
);
},
),
);
}
}
我正在尝试清理这些乱七八糟的小部件,但我找不到这样做的方法。我的 NavigationBloc
依赖于 AuthenticationBloc
提供的流,为了防止内存泄漏,我必须关闭流。
需要 Builder 小部件,以便我可以获得 BlocProvider
提供的最新 BuildContext
,但我知道 MultiBlocProvider
会极大地清理它。我想避免将此小部件包装在 runApp
函数中,但我猜这是一个选项。
class _MyAppState extends State<MyApp> {
final authRepo = AuthRepo();
AuthenticationBloc authBloc;
@override
void dispose() {
authBloc?.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlocProvider<AuthenticationBloc>(
create: (_) =>
AuthenticationBloc(authRepo: authRepo)..add(InitializeAuth()),
child: Builder(builder: (context) {
authBloc = context.bloc<AuthenticationBloc>();
return BlocProvider<NavigationBloc>(
create: (_) => NavigationBloc(authBloc),
child: MaterialApp(
title: 'Arrow Manager',
debugShowCheckedModeBanner: false,
theme: appTheme(),
builder:
ExtendedNavigator<Router>(router: Router(), initialRoute: '/'),
),
);
}),
);
}
}
正如您所说,您可以使用 MultiProvider
来避免嵌套提供程序
您必须在 initState()
方法中创建 AuthenticationBloc
class _MyAppState extends State<MyApp> {
final authRepo = AuthRepo();
AuthenticationBloc authBloc;
@override
void initState() {
super.initState();
authBloc = AuthenticationBloc(authRepo: authRepo);
}
@override
void dispose() {
authBloc?.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (_) => authBloc..add(InitializeAuth()),
),
BlocProvider(
create: (context) => NavigationBloc(authBloc),
),
],
child: Builder(
builder: (context) {
authBloc = context.bloc<AuthenticationBloc>();
return MaterialApp(
title: 'Arrow Manager',
debugShowCheckedModeBanner: false,
theme: appTheme(),
builder: ExtendedNavigator<Router>(router: Router(), initialRoute: '/'),
);
},
),
);
}
}