我刚刚注意到,每当我热重启我的应用程序时,Chart
的实例似乎就会堆积起来。我已附上一张图像,该图像显示每次热重启时,都会添加一个AuthBloc
的实例。
以下是我在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());
}
}
实例不会堆积,我该怎么做?