Bloc: Provide initial value to the BlocBuilder

Created on 22 Jul 2020  路  4Comments  路  Source: felangel/bloc

Want initial value to prevent null state when BlocBuilder fist time built

For multiple page that use the same stream will have a null state when they build first time, and refresh child after bloc updated.

How I fix that for now

The solution now I made is to keep a state last time I updated, and in each widget which will use that bloc will be update in the init() method by the last state in the stream, and next fetch the new state.
This can be used as a "Fake Initial value" to prevent initial null state and I can get visual what data I have so far and don't need to wait for the new fetch

void聽updateUserBloc()聽{
聽聽if聽(userinfo聽!= null)聽{
聽聽聽聽userInfoBloc.update(Future.value(userinfo));
聽聽}

聽聽userInfoBloc.update(memberApi.queryByIdUsingPOST());
}

ResultBase<UserInfo>聽userinfo;
question

All 4 comments

What does your UserInfoBloc look like? It sounds like you are not providing it with an initial state?

What does your UserInfoBloc look like? It sounds like you are not providing it with an initial state?

It's basically we have a login screen and it will update the bloc user info and the home widget can show the states. Please have look on the bloc that I basically copy and modified from the examples.

But is the RxBlocBuilder can take an initial value? I don't see that from the implementation.

In each new widget that we may want to update the user info I will call to update it, and without the incoming user info, the RxBlocBuilder will have a null state.

My bloc

abstract聽class聽UserInfoBlocEvents聽{
聽聽void聽update(Future<ResultBase<UserInfo>>聽userInfo);
}

abstract聽class聽UserInfoBlocStates聽{
聽聽Stream<ResultBase<UserInfo>>聽get聽userinfo;

聽聽@RxBlocIgnoreState()
聽聽Stream<LoginStatus>聽get聽isLoggedIn;

聽聽@RxBlocIgnoreState()
聽聽Stream<bool>聽get聽isLoading;

聽聽@RxBlocIgnoreState()
聽聽Stream<String>聽get聽errors;

聽聽//聽聽We聽need聽to聽keep聽a聽token聽where聽some聽method聽won't聽return聽a聽token
聽聽@RxBlocIgnoreState()
聽聽Stream<UserToken>聽get聽userToken;
}

@RxBloc()
class聽UserInfoBloc聽extends聽$UserInfoBloc聽{
聽聽@override
聽聽Stream<ResultBase<UserInfo>>聽_mapToUserinfoState()聽{
聽聽聽聽return聽_$updateEvent
聽聽聽聽聽聽聽聽.switchMap((x)聽=>聽x.asResultStream())
聽聽聽聽聽聽聽聽.setResultStateHandler(this)
聽聽聽聽聽聽聽聽.whereSuccess();
聽聽}

聽聽@override
聽聽Stream<String>聽get聽errors聽=>聽errorState.skip(1).map((exception)聽{
聽聽聽聽聽聽聽聽String聽msg聽=聽exception.toString();
聽聽聽聽聽聽聽聽if聽(msg.contains('Exception:'))聽msg聽=聽msg.replaceAll('Exception:',聽'');
聽聽聽聽聽聽聽聽return聽msg;
聽聽聽聽聽聽});

聽聽@override
聽聽Stream<bool>聽get聽isLoading聽=>聽loadingState;

聽聽@override
聽聽Stream<LoginStatus>聽get聽isLoggedIn聽{
聽聽聽聽var聽stream聽=聽states.userinfo.asBroadcastStream();

聽聽聽聽var聽fail聽=聽stream
聽聽聽聽聽聽聽聽.where((x)聽=>聽x?.success聽==聽false)
聽聽聽聽聽聽聽聽.switchMap((_)聽=>聽Future.value(LoginStatus.NotLogin).asStream());

聽聽聽聽var聽success聽=聽stream
聽聽聽聽聽聽聽聽.where((x)聽=>聽x?.success聽==聽true聽&&聽x?.code聽==聽200)
聽聽聽聽聽聽聽聽.where((x)聽=>聽x?.result聽!=聽null)
聽聽聽聽聽聽聽聽.where((x)聽=>聽x?.result?.token聽!=聽null)
聽聽聽聽聽聽聽聽.switchMap((_)聽=>聽Future.value(LoginStatus.LoggedIn).asStream());

聽聽聽聽return聽fail.mergeWith([success]).startWith(LoginStatus.NotLogin);
聽聽}

聽聽@override
聽聽Stream<UserToken>聽get聽userToken聽=>聽states.userinfo
聽聽聽聽聽聽.asBroadcastStream()
聽聽聽聽聽聽.where((x)聽=>聽x?.result?.token聽!=聽null)
聽聽聽聽聽聽.switchMap((x)聽=>聽Future.value(x.result.token).asStream());

聽聽///聽Closes聽the聽[Event]聽and聽[State]聽[Stream]s.
聽聽@mustCallSuper
聽聽void聽dispose()聽async聽{
聽聽聽聽await聽_$updateEvent.drain();
聽聽聽聽_$updateEvent.close();
聽聽聽聽super.dispose();
聽聽}
}

///聽Status聽determines聽the聽application聽login聽status
enum聽LoginStatus聽{聽

How I use the RxBlocBuilder

RxBlocBuilder<UserInfoBlocType,聽ResultBase<UserInfo>>(
state:聽(bloc)聽=>聽bloc.states.userinfo,)

Hmm, I did not know there was an RxBloc package.
I think you are in the wrong GitHub repo. This is for the Bloc package, https://bloclibrary.dev/.
Are you looking for https://github.com/Prime-Holding/FlutterRxBloc ?

Hmm, I did not know there was an RxBloc package.
I think you are in the wrong GitHub repo. This is for the Bloc package, https://bloclibrary.dev/.
Are you looking for https://github.com/Prime-Holding/FlutterRxBloc ?

Oh god, I'm open an issue at the wrong chrome tab :)
Sorry for the bother~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clicksocial picture clicksocial  路  3Comments

timtraversy picture timtraversy  路  3Comments

nhwilly picture nhwilly  路  3Comments

craiglabenz picture craiglabenz  路  3Comments

komapeb picture komapeb  路  3Comments