我正在尝试清理这些乱七八糟的小部件,但我没有办法这样做。我NavigationBloc
依赖于提供的流AuthenticationBloc
,为了防止内存泄漏,我必须关闭流。
Builder 小部件是必需的,这样我才能获得由BuildContext
提供的最新信息,BlocProvider
但我知道这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: '/'),
),
);
}),
);
}
}