什么时候将集团置于混乱之中?

问题描述

在下面的示例中,我们可以看到在这个有状态的小部件中新创建了一个区块

authenticationBloc = AuthenticationBloc(userRepository: userRepository);

class App extends StatefulWidget {
  final UserRepository userRepository;

  App({Key key,@required this.userRepository}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  AuthenticationBloc authenticationBloc;
  UserRepository get userRepository => widget.userRepository;

  @override
  void initState() {
    authenticationBloc = AuthenticationBloc(userRepository: userRepository); <------
    authenticationBloc.dispatch(AppStarted());
    super.initState();
  }

  @override
  void dispose() {                     <---------
    authenticationBloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider<AuthenticationBloc>(
      bloc: authenticationBloc,child: MaterialApp(
        home: BlocBuilder<AuthenticationEvent,AuthenticationState>(
          bloc: authenticationBloc,builder: (BuildContext context,AuthenticationState state) {
            if (state is AuthenticationUninitialized) {
              return SplashPage();
            }
            if (state is AuthenticationAuthenticated) {
              return HomePage();
            }
            if (state is AuthenticationUnauthenticated) {
              return LoginPage(userRepository: userRepository);
            }
            if (state is AuthenticationLoading) {
              return LoadingIndicator();
            }
          },),);
  }
}

但是随后它也被放置在相同的有状态小部件中

 @override
  void dispose() {                     
    authenticationBloc.dispose(); <-----
    super.dispose();
  }

现在在子小部件HomePage()中,如果authenticationBloc statefulwidget中已经放置了BlocProvider.of<AuthenticationBloc>(context),我怎么仍能使用App访问authenticationBloc.dispose();function myFunction(){ $dbinfo = get_db_info(); $users = $dbinfo->get_results(" SELECT Email,ID,Name,Address FROM Database_table WHERE ID = 5 "); if($users) { $output = ''; foreach($users as $user) { $ID = $user->ID; $dname = $user->Name; $email = $user->Email; $address = $user->Address; }else{ $output .= 'No user found'; } }//endif return $output; } 是否正在关闭水槽?还是我理解这个错误

解决方法

当应从树中永久删除_AppState且永远不会再构建State对象时,将调用dispose方法。

因此,由于仍在构建子小部件,因此尚未运行您的dispose方法。