Cretezy / redux_persist

Persist Redux State
https://pub.dartlang.org/packages/redux_persist
MIT License
130 stars 41 forks source link

0.7.0-rc.1 persistor.createMiddleware() not correct subtype #6

Closed igoooor closed 6 years ago

igoooor commented 6 years ago

When running the following portion of code

final store = Store<AppState>(
  reducer,
  initialState: AppState(),
  middleware: [persistor.createMiddleware()],
);

I receive the following error type '(Store<AppState>, dynamic, (dynamic) => void) => Future<Null>' is not a subtype of type '(Store<dynamic>, dynamic, (dynamic) => void) => void'

Here are my dependencies

flutter_redux: "^0.5.0"
redux_persist: "^0.7.0-rc.1"
redux_persist_flutter: "^0.6.0-rc.1"

And I correctly followed all migration steps (yours and the one of flutter_redux). On a side note, I had it perfectly working until the migration of my code to Dart 2 and your RC.

Cretezy commented 6 years ago

Can you give me the line number which throws this error?

igoooor commented 6 years ago

it comes from the function Persistor.createMiddleware(), line 77

Cretezy commented 6 years ago

Published 0.7.0-rc.2. Should fix this.

Let me know if everything is good! If so, I'll push it as 0.7.0.

imtoori commented 6 years ago

I have the same problem with 0.7.0-rc2 :/

the error is a bit different type '(Store<AppState>, dynamic, (dynamic) => void) => Null' is not a subtype of type '(Store<dynamic>, dynamic, (dynamic) => void) => void'

Cretezy commented 6 years ago

Seems like you are returning Null somewhere instead of void?

imtoori commented 6 years ago

Me? I don't think. I think the reason is that the package uses dart redux 2.x and I use redux 3.x

ianrothmann commented 6 years ago

Were you able to solve this? I'm having the exact same issue here.

type '(Store, dynamic, (dynamic) => void) => Null' is not a subtype of type '(Store, dynamic, (dynamic) => void) => void'

When calling: persistor.createMiddleware()

Cretezy commented 6 years ago

This is very odd, as I can't really reproduce this.

Can you give me a full example that I can run to reproduce this?

ianrothmann commented 6 years ago

If I change this (in redux_persist.dart):

Middleware<T> createMiddleware() =>
      (Store<T> store, dynamic action, NextDispatcher next) {
        next(action);

        if (action is! PersistAction) {
          try {
            // Save
            save(store);
          } catch (_) {}
        }
      };

to this:

Middleware<T> createMiddleware(){
    return middlewareHandler;
  }

  void middlewareHandler
      (Store<T> store, dynamic action, NextDispatcher next) {
        next(action);

        if (action is! PersistAction) {
          try {
            // Save
            save(store);
          } catch (_) {}
        }
  }

The error goes away.

Using: flutter 0.5.1, redux 3.0.0

Cretezy commented 6 years ago

Hey! Try again using the latest versions and let me know how it goes for you!

diogo-ribeiro-bitmaker commented 6 years ago

Hey again @Cretezy! I'm having this problem now. My code is divided in two files and that seems to be an issue:

And if I do this, it starts to work:

  final Persistor<AppState> persistor = Persistor<AppState>(
    storage: FlutterStorage(key: 'flutter_boot'),
    serializer: JsonSerializer<AppState>(AppState.persistorSerializer),
  );

  final AppState initial = await persistor.load();

  final Store<AppState> store = Store(
    appReducer,
    middleware: [
      persistor.createMiddleware(),
      thunkMiddleware,
      new LoggingMiddleware.printer(
        formatter: LoggingMiddleware.multiLineFormatter,
      ),
    ],
    initialState: initial ?? AppState(),
  );

  return store;
}

The problem seems to be that the persistor.createMiddleware() needs to be in the same file as the creation of the store but that doesn't make sense. Could you help please? Thanks! :)

tynn commented 3 years ago

I had the same issue recently, but it was a problem with generics. I used the type as Persistor instead of Persistor<AppState>. Declaring the generic type made the difference.

elvisun commented 3 years ago

Having the same issue with the following code:

    return Store(
      appReducer,
      middleware: Middleware<AppState>[
        persistor.createMiddleware(),
        ...createMiddlewares(injectedServices),
      ],
      initialState: initialState ?? AppState.defaultState(),
      distinct: true,
    );

I had persistor declared in another file and passed it over.

Once I moved persistor into the same file the issue went away ¯_(ツ)_/¯