Flutter Bloc会阻止在热重启时出现多个实例?

时间:2019-12-05 14:20:39

标签: flutter dart bloc flutter-bloc

我刚刚注意到,每当我热重启我的应用程序时,Chart的实例似乎就会堆积起来。我已附上一张图像,该图像显示每次热重启时,都会添加一个AuthBloc的实例。

enter image description here

以下是我在AuthBloc上的代码:

main.dart

为了重新启动热启动时void main() { BlocSupervisor.delegate = SimpleBlocDelegate(); final userRepository = UserRepository(); runApp( BlocProvider<AuthBloc>( create: (context) { return AuthBloc(userRepository: userRepository); }, child: MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => UserProvider()), ChangeNotifierProvider(create: (context) => UserTokenProvider()), ], child: MainApp(userRepository: userRepository), ), ), ); } class MainApp extends StatefulWidget { final UserRepository userRepository; const MainApp({Key key, @required this.userRepository}) : super(key: key); @override _MainAppState createState() => _MainAppState(); } class _MainAppState extends State<MainApp> { @override void initState() { super.initState(); // WidgetsBinding.instance.addPostFrameCallback( // (_) => new FirebaseMessagingController().initialize()); WidgetsBinding.instance.addPostFrameCallback((_) => _initializeAppStart()); } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Center( child: LoginPage( userRepository: widget.userRepository, ), ), ); } _initializeAppStart() { BlocProvider.of<AuthBloc>(context).add(AppStarted()); } } 实例不会堆积,我该怎么做?

0 个答案:

没有答案