brianegan / redux_thunk

Redux Middleware for handling functions as actions
MIT License
88 stars 8 forks source link

Dispatch never calls ThunkAction function body #22

Closed bleszerd closed 1 year ago

bleszerd commented 1 year ago

I'm trying to follow the example syntax on the repository homepage, but without success. Look:

ThunkAction<AppState> getGeolocationData() {
  return (Store<AppState> store) async {
    // The lines below were never called;
    try {
      // The origin of "getGeolocationUsecase" doesn't matter in this example
      GeolocationDataEntity geolocation = await getGeolocationUsecase();

      store.dispatch(
        UpdateGeolocationDataAction(geolocation),
      );
      print(geolocation);
    } catch (e) {
      print(e.toString());
    }

    // Printed!
    print("Print call!");
  };
}
// Widget render method
Scaffold(
  body: StoreConnector<AppState, AppState>(
    onInit: (store) {
      store.dispatch(getGeolocationData());
    },
    converter: (Store<AppState> store) => store.state,
    builder: (context, state) => // ... App widgets

Try/catch lines on getGeolocationData are never called.

The closest I got was directly calling the internal function's call() method and providing the store like this:

Scaffold(
  body: StoreConnector<AppState, AppState>(
    onInit: (store) {
      store.dispatch(getGeolocationData()(store));
     // or store.dispatch(getGeolocationData().call(store));
    },
    converter: (Store<AppState> store) => store.state,
    builder: (context, state) => // ... App widgets

Maybe it's unexpected behavior or I'm doing it wrong. In case of doubt, I decided to open an issue so that someone could help me.

brianegan commented 1 year ago

Hey hey 👋

Thanks for the report. A couple of quick questions:

  1. have you included the thunkMiddleware in the list of middleware provided to your Store? For example:
final store = new Store<String>(
  reducer,
  middleware: [thunkMiddleware],
);
  1. Is the function ever called, or is it only the try/catch part that fails? This could be verified with a breakpoint or print statement, whichever you'd prefer!

Thanks, Brian

bleszerd commented 1 year ago

Let's take a closer look

  1. This is my store

    final appStore = Store<AppState>(
    combineReducers([authReducer]),
    initialState: AppState.initialState(),
    middleware: [thunkMiddleware],
    );
  2. Yes, the function is called but it doesn't go into try/catch. The line below the comment "Printed!" in the example above is displayed in the logs

These are all my project dependencies:

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  flutter_svg: ^1.1.5
  omni_datetime_picker: ^0.1.2
  geolocator: ^9.0.2
  dropdown_search: ^5.0.3
  dio: ^4.0.6
  flutter_redux: ^0.10.0
  redux: ^5.0.0
  flutter_redux_dev_tools: ^0.5.2
  redux_thunk: ^0.4.0

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^2.0.0

Thanks for the help, let me know if you need more information 🚀.

bleszerd commented 1 year ago

I just realized that I imported the wrong store that didn't contain thunkMiddleware as middleware and I ended up not realizing it. I apologize if I wasted your time on this.

Thank you again for your help 😅!