flutter 如何屈服于块流?

问题描述 投票:0回答:2

嗨,我是 flutter 和 dart 新手。我正在网上学习一堂课,练习使用集团来控制国家。第一课是展示appStart动画后,转到登录页面。

本课使用“mapEventToState”:

class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState> {
    final UserRepository? _userRepository;

    AuthenticationBloc({UserRepository? userRepository})
    : assert(userRepository != null),
    _userRepository = userRepository, super(Uninitialized());
    @override
    Stream<AuthenticationState> mapEventToState(
        AuthenticationEvent event,
    ) async* {
        if (event is AppStarted) {
            yield* _mapAppStartedToState();
        } else if (event is LoggedIn) {
            yield* _mapLoggedInToState();
        } else if (event is LoggedOut) {
            yield* _mapLoggedOutToState();
        }
    Stream<AuthenticationState> _mapAppStartedToState() async* {
        log('_mapAppStartedToState is running.');
        try {
            final bool? isSigned = await _userRepository?.isSignedIn();
            if (isSigned != null) {
                if (isSigned) {
                    final String? name = await _userRepository?.getUser();
                    yield Authenticated(name);
                }
                else {
                    yield Unauthenticated();
                }
            }
        } catch (_) {
            yield Unauthenticated();
        }
    }

    Stream<AuthenticationState> _mapLoggedInToState() async* {
        log('_mapLoggedInToState is running.');
        yield Authenticated(await _userRepository?.getUser());
    }

    Stream<AuthenticationState> _mapLoggedOutToState() async* {
        log('_mapLoggedOutToState is running.');
        yield Unauthenticated();
        _userRepository?.signOut();
    }
}

结果“mapEventToState”被删除了。 根据此页面(https://github.com/felangel/bloc/issues/2526),我尝试使用on< event >代替:

@override
AuthenticationBloc({UserRepository? userRepository})
: assert(userRepository != null, 'userRepository == null'),
    _userRepository = userRepository,
    super(Uninitialized()) {
        log('AuthenticationBloc is running.');
        on<AppStarted>(_appStarted);
        on<LoggedIn>(_loggedIn);
        on<LoggedOut>(_loggedOut);
    }
Stream<AuthenticationState> _appStarted(AuthenticationEvent event, Emitter<AuthenticationState> emit) async* {
    log('_appStarted is running.');
    yield* _mapAppStartedToState();
}

但是没有成功。甚至

log('_appStarted is running.');
也没有显示在控制台上。

我尝试更改类型和 aync*。如果 _appStarted 不是 aync,它将显示控制台日志。

void _appStarted(AuthenticationEvent event, Emitter<AuthenticationState> emit) {
    log('_appStarted is running.');
    // yield* _mapAppStartedToState();
}

但是,它无法产生流,因为 _appStarted 不是 aync。让我很困惑。

如果我对 bloc 和 Stream 有任何误解,请告诉我。很高兴看到任何解决方案或建议。

flutter dart stream state bloc
2个回答
1
投票

您不再需要每个事件一个功能,因为您已经拥有它:

void _appStarted(AuthenticationEvent event, Emitter<AuthenticationState> emit) {
    log('_appStarted is running.');
     try {
            final bool? isSigned = await _userRepository?.isSignedIn();
            if (isSigned != null) {
                if (isSigned) {
                    final String? name = await _userRepository?.getUser();
                    emit(Authenticated(name));
                }
                else {
                    emit(Unauthenticated());
                }
            }
        } catch (_) {
            emit(Unauthenticated());
        }
}

如果您想要将其委托给另一个函数,只需删除流返回值并传递发射器即可。


0
投票

 bloc v8.0.0
开始,有几个API被移除:

  • mapEventToState 被删除,取而代之的是 on
  • transformEvents 被删除,转而使用 EventTransformer API
  • 删除了 TransitionFunction typedef 以支持 EventTransformer API
  • listen 被删除,取而代之的是stream.listen

可以参考这个文档: https://bloclibrary.dev/migration/#-remove-deprecated-apis

© www.soinside.com 2019 - 2024. All rights reserved.