marcglasberg / async_redux

Flutter Package: A Redux version tailored for Flutter, which is easy to learn, to use, to test, and has no boilerplate. Allows for both sync and async reducers.
Other
234 stars 40 forks source link

StoreConnector how to handle null returned by the model/converter #19

Closed SunlightBro closed 5 years ago

SunlightBro commented 5 years ago

I quite often run into this nuisance, that the first thing in builder function is to check if the Model is null, as some values in the AppState are nullable (e.g. after successful login the User's details are requested via an UserGet ReduxAction, before that AppState().self is just null).

return StoreConnector<AppState, User>(
  converter: (store) => store.state.self,
  builder: (context, self) {
    return self == null 
        ? CircularProgressIndicator()
        : CircleAvatar(backgroundImage: CachedNetworkImageProvider(self.imageUrl)),
  }
);

Is this the intended way or should the model or converter function not be able to return null ?

marcglasberg commented 5 years ago

Usually the ViewModel would be a class containing the nullable state piece. What you want is the state piece itself to be the ViewModel. I never thought of using it that way, but I guess it's a valid way of using it. It should be easy to fix this.

What is the error you're getting when you try to do this?

SunlightBro commented 5 years ago

Ok misphrased this a bit, the above mentioned example works fine, as I'm using built_value for all my models I can always be sure that ==() and hashCode are implemented.

But as models get more complicated getting a model can become pretty ugly and error prone.

StoreConnector<AppState, String>(
  converter: (store) => store.state.pullrequest?.author?.links?.avatar?.href,
  builder: (context, href) {...},

The more I think about this, I should just stop being lazy and start using proper ViewModels.

I'll close this issue.