简体   繁体   English

扑。 这个流订阅会被取消吗?

[英]Flutter. Will this streamsubscription get cancelled?

I am developing an application in Flutter and in one instance, using a stream subcscription.我正在 Flutter 中开发一个应用程序,在一个实例中,使用流订阅。 I have some concerns about memory leaks, as I am not sure if my close() method will ever get called.我对内存泄漏有些担心,因为我不确定我的 close() 方法是否会被调用。

Previously this wasn't a concern, but due to an update to flutter_bloc, the structure of a file has been changed.以前这不是问题,但由于更新了flutter_bloc,文件的结构已经改变。

Previously my code looked like this:以前我的代码如下所示:

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  final AuthRepository _authRepository;
  late StreamSubscription<auth.User?> _userSubscription;

  AuthBloc({
    required AuthRepository authRepository,
  })  : _authRepository = authRepository,
        super(AuthState.unknown()) {
    _userSubscription =
        _authRepository.user.listen((user) => add(AuthUserChanged(user: user)));
  }

  @override
  Future<void> close() {
    _userSubscription.cancel();
    return super.close();
  }

  @override
  Stream<AuthState> mapEventToState(AuthEvent event) async* {

You can see the beginning of mapEventToState comes after the closing brackets of the constructor.您可以看到 mapEventToState 的开头出现在构造函数的右括号之后。

Currently, with the update to flutter_bloc 7.3, my code looks like this:目前,随着flutter_bloc 7.3的更新,我的代码如下所示:

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  final AuthRepository _authRepository;
  late StreamSubscription<auth.User?> _userSubscription;
  AuthBloc({
    required ...,
  })  : _authRepository = ...,
        super(...) {

    _userSubscription =
        _authRepository.user.listen((user) => add(AuthUserChanged(user: user)));

    on<AuthUserChanged>((event, emit) {
      event.user != null
          ? emit(AuthState.authenticated(user: event.user!))
          : emit(AuthState.unauthenticated());
    });
    on<AuthLogoutRequested>((event, emit) async {
      await _authRepository.logOut();
    });
  }

  @override
  Future<void> close() {
    _userSubscription.cancel();
    return super.close();
  }
}

mapEventToState was replaced with "on", and as you can see it is defined after the constructor in the same block as the streamsubscription instantiaion. mapEventToState 被替换为“on”,正如您所看到的,它是在与流订阅实例相同的块中的构造函数之后定义的。 My code works and these events fire, but my close method is at the bottom now due to this new structure.我的代码可以工作并且这些事件会触发,但是由于这种新结构,我的 close 方法现在位于底部。 Can anybody tell me if this method will ever get called?谁能告诉我这个方法是否会被调用?

I've put print statements inside of it and they never seem to print, but even in the previous version print statements inside of close() would not print.我已经将打印语句放入其中,但它们似乎从未打印,但即使在以前的版本中,close() 中的打印语句也不会打印。 Does anybody know if close() will get called in this new code structure?有人知道在这个新的代码结构中是否会调用 close() 吗?

Actually, stream subscription must be working on the updated version of BloC.实际上,流订阅必须在 BloC 的更新版本上工作。 See more on https://github.com/felangel/bloc/issues/2890https://github.com/felangel/bloc/issues/2890上查看更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM