brianegan / flutter_redux

A library that connects Widgets to a Redux Store
MIT License
1.65k stars 219 forks source link

reducer not getting called #217

Closed Zzerr0r closed 3 years ago

Zzerr0r commented 3 years ago

This is my first time working with flutter_redux so it might be a really simple oversight but whenever i provide a middleware in the store-initialization, my reducer isnt getting called after the middleware. When i don't provide a middleware, the reducer is getting called.

main.dart

void main() {
  Store<AppState> store = Store<AppState>(reducer, initialState: AppState.initial(), middleware: [appStateMiddleWare]);

  runApp(MyApp(store));
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  MyApp(this.store);

  final Store<AppState> store;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: StoreProvider<AppState>(
          store: store,
          child: MyHomePage(store: store)),
    );
  }
}

middleware.dart (part of it)

void appStateMiddleWare(
    Store<AppState> store, dynamic event, NextDispatcher next) {
  final state = store.state;

  if (event is MapTapEvent) {
    if(state.showCake || state.showInfo){
      store.dispatch(UpdateShowCakeAndShowInfoAction(showCake: false, showInfo: false)); //this statement gets reached
    }else{
      store.dispatch(NewCakeAction(event.tapPosition),);
    }
  }
}

reducer.dart

AppState reducer(AppState pre, dynamic action){
  print("in the reducer"); //this statement never gets reached
  if(action is UpdateCakePositionAction) return AppState.copyWith(pre: pre, cakePosition: action.cakePosition);
  else if (action is SelectCakeIconAction) return AppState.copyWith(pre: pre, selectedPiece: action.selectedIcon);
  else if (action is UpdateLastLabelBoundsAction) return AppState.copyWith(pre: pre, lastLabelBounds: action.lastLabelBounds);
  else if (action is UpdateReloadLabelsAction) return AppState.copyWith(pre: pre, reloadLabels: action.reloadLabels);
  else if (action is UpdateShowCakeAction) return AppState.copyWith(pre: pre, showCake: action.showCake);
  else if (action is UpdateShowInfoAction) return AppState.copyWith(pre: pre, showInfo: action.showInfo);
  else if (action is UpdateMapBoundsAction) return AppState.copyWith(pre: pre, mapBounds: action.mapBounds);
  else if (action is UpdateMapZoomAction) return AppState.copyWith(pre: pre, mapZoom: action.mapZoom);
  else if (action is UpdateSelectedPieceAction) return AppState.copyWith(pre: pre, selectedPiece: action.selectedPiece);
  else if (action is UpdateShowCakeAndShowInfoAction) return AppState.copyWith(pre: pre, showInfo: action.showInfo, showCake: action.showCake);
  else if (action is RemoveCakeAction) return AppState.copyWith(pre: pre, showCake: false, selectedPiece: NullIcon());
  else if (action is UpdateWayPointsAction) return AppState.copyWith(pre: pre, wayPoints: action.wayPoints);
  else if (action is ReloadMapAction) return AppState.copyWith(pre: pre, reloadLabels: true, reloadMarkers: true);
  else if (action is MapInitializedAction) return AppState.copyWith(pre: pre, mapInitialized: true);
  else if (action is StopVideoAction) return AppState.copyWith(pre: pre, showVideo: false, showVideoControls: false);
  else if (action is UpdateVideoFullScreenAction) return AppState.copyWith(pre: pre, videoFullScreen: action.videoFullScreen, showVideoControls: false);
  else if (action is UpdateVideoPausedAction) return AppState.copyWith(pre: pre, videoPaused: action.videoPaused);
  else print("no action matched in reducer");
  return pre;
}
Zzerr0r commented 3 years ago

I forgot to call next(event) in the middleware