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
230 stars 41 forks source link

Calling WaitAction on subclassed State #78

Closed leonardo2204 closed 4 years ago

leonardo2204 commented 4 years ago

Hey @marcglasberg, first of all, thanks for the great work here!

I'm trying to figure out how can I call before/after with WithAction on a subclassed state (https://github.com/marcglasberg/async_redux#action-subclassing).

Using the example in the readme, that would be:

class AddTodoAction extends TodoAction {
  final Todo todo;
  AddTodoAction(this.todo);

  @override
  void before() => dispatch(WaitAction.add(1));

  @override
  void after() => dispatch(WaitAction.remove(1));

  @override
  TodoState reduceTodoState() {
    if (todo == null) return null;
    else return List.of(todos)..add(todo);
  }
}

Doing so I get:

E/flutter (11587): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Assertion failed: "The store state is not compatible with WaitAction."
E/flutter (11587): #0      WaitAction._defaultReducer.<anonymous closure> (package:async_redux/src/wait_action.dart:59:11)
E/flutter (11587): #1      WaitAction.reduce (package:async_redux/src/wait_action.dart:107:25)
E/flutter (11587): #2      Store._applyReducer (package:async_redux/src/store.dart:461:25)
E/flutter (11587): #3      Store._processAction (package:async_redux/src/store.dart:435:16)
E/flutter (11587): #4      Store.dispatch (package:async_redux/src/store.dart:373:5)
E/flutter (11587): #5      FetchAllEventSummaries.after (package:app/redux/event_summary/actions/fetch_all_event_summaries.dart:15:27)

Is it possible to make before() and after() dispatch from the right state?

Valeu Marcelo 🇧🇷

marcglasberg commented 4 years ago

Oi Leonardo. Esse erro:

   "The store state is not compatible with WaitAction."

Significa que você não preparou o seu state corretamente. Veja essa explicação:

For this to work, your store state must have a Wait field named wait, and then your state class must have a copy or a copyWith method which copies this field as a named parameter. For example:

class AppState {
  final Wait wait;
  ...

  AppState({this.wait, ...});

  AppState copy({Wait wait, ...}) => AppState(wait: wait, ...);
  }

Ou seja, o seu state precisa declarar o campo wait, e também o método copy, tal como mostrado acima. Se você ainda tiver problemas, cole aqui pra mim o código da sua classe AppState.