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

How to extract part of Actions into separate module (package) #60

Closed jans-y closed 4 years ago

jans-y commented 4 years ago

Hi,

We are working on two very similar apps sharing a lot of logic and a common backend.

We would like to extract our login screens and corresponding logic (actions) into a package and import it into both apps.

The problem is how to get the app's AppState inside the package logic:

class LoginAction extends ReduxAction<AppState>

The only solution we came up with is to have a separate store inside the package something like LoginState and then use it like this

module:

class LoginAction extends ReduxAction<LoginState>

app:

class AppState {
    LoginState get loginState => loginStore.state;
}

Thank you.

marcglasberg commented 4 years ago

The state, Actions and Connectors get coupled in AsyncRedux, I don't see a way around it. Maybe there is, but since I have never tried to do what you describe, I couldn't give you an answer without studying the problem in detail, which I can't do right now.

At first glance I don't see a problem on creating a different store for the login sequence, since it's very separate from the rest of the program. If it were not, your approach definitely wouldn't work, since LoginState get loginState => loginStore.state; would act like a mutable state in the eyes of the main store. This means, if your LoginState is changed by your login-store, the main store wouldn't know about this change, and wouldn't rebuild the widgets, for example.

Maybe a better approach is to have the two different stores completely apart. When the login sequence finishes it can call store.setLoginStateAction(loginStore.state) to put the state into the normal store.

Fun fact: When I was first learning about Redux I had a kind of similar question: https://stackoverflow.com/questions/49621633/using-flutter-and-redux-flutter-redux-how-can-i-uncouple-widgets-from-the-app