简体   繁体   English

flutter 如何屈服于 stream 块?

[英]flutter how to yield to a stream of bloc?

Hi I'm new to flutter and dart.嗨,我是 flutter 和 dart 的新手。 I'm following a lesson on internet which is practicing to use bloc to control states.我正在学习互联网上的课程,该课程正在练习使用 bloc 来控制状态。 First lesson is after showing appStart animation, turn to a login page.第一课是在显示 appStart animation 之后,转到登录页面。

the lesson was using 'mapEventToState':本课使用“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();
    }
}

turns out 'mapEventToState' was removed.原来“mapEventToState”已被删除。 According to this page( https://github.com/felangel/bloc/issues/2526 ), I try to use on< event > instead:根据此页面( 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();
}

But it didn't work.但它没有用。 Even log('_appStarted is running.');甚至log('_appStarted is running.'); didn't show at console.没有在控制台显示。

I tried to change type and aync*.我试图改变类型和 aync*。 It would show console log if _appStarted isn't aync.如果 _appStarted 不是 aync,它将显示控制台日志。

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

However, it can't yield to stream as _appStarted isn't aync.但是,它不能屈服于 stream,因为 _appStarted 不是 aync。 Makes me confused.让我很困惑。

Please let me know if I got some misunderstand about bloc and stream.如果我对 bloc 和 stream 有一些误解,请告诉我。 Happy to see any solution or advise.很高兴看到任何解决方案或建议。

You no longer need your one function per event, because you already have it:您不再需要每个活动的 function,因为您已经拥有它:

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());
        }
}

If you want to delegate this to another function, just remove the stream return value and pass the emitter.如果您将此委托给另一个 function,只需删除 stream 返回值并传递发射器即可。

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

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