Closed jaggerwang closed 6 years ago
Hey there! Thanks for writing in... I think the problem is that you're not giving Dart2 enough type information to correctly infer what's contained in the mws
list.
Could you please change var mws = [persistor.createMiddleware()];
to var mws = <Middleware<AppState>>[persistor.createMiddleware()];
and see if that fixes the issue? If that doesn't do it, could you please link to full source so I can take a closer look?
Both this:
List<Middleware<AppState>> mws = [persistor.createMiddleware()];
mws.addAll(createMiddlewares());
mws.add(LoggingMiddleware.printer());
and this:
var mws = <Middleware<AppState>>[persistor.createMiddleware()];
mws.addAll(createMiddlewares());
mws.add(LoggingMiddleware.printer());
is not working.
I think it is because the return type of printer()
is LoggingMiddleware<State>
, not (Store<AppState>, dynamic, (dynamic) → void) → void
. There is a warning in VSCode.
[dart] The argument type 'List<LoggingMiddleware>' can't be assigned to the parameter type 'List<(Store<AppState>, dynamic, (dynamic) → void) → void>'.
LoggingMiddleware<State>
is a generic type... therefore you can pass through your own type info for it: LoggingMiddleware<AppState>.printer()
This is working for me:
final middleware = <Middleware<SearchState>>[
SearchMiddleware(GithubApi()),
];
middleware.add(LoggingMiddleware.printer());
But if sounds like you might need to provide the extra type info to the LoggingMiddleware
constructor if that's not working on your side:
final middleware = <Middleware<SearchState>>[
SearchMiddleware(GithubApi()),
];
middleware.add(LoggingMiddleware<SearchState>.printer());
If this isn't working, could you please provide a repo or full source? It's difficult to debug type problems with code snippets since I can't see the full type info from my side. Also, which version of Flutter are you on? This is working for me on the beta channel, haven't tried master and the Dart 2 runtime is changing.
Thanks! This worked for me:
store = store ??
Store<AppState>(
appReducer,
initialState: AppState(),
middleware: [persistor.createMiddleware()] +
createMiddlewares() +
<Middleware<AppState>>[LoggingMiddleware.printer()],
);
But not this:
store = store ??
Store<AppState>(
appReducer,
initialState: AppState(),
middleware: [persistor.createMiddleware()] +
createMiddlewares() +
[LoggingMiddleware<AppState>.printer()],
);
It should works too. I already specified the generic param type AppState
, and printer()
should return type (Store<AppState>, dynamic, (dynamic) → void) → void
.
Ah, yah, it is a bit confusing... it's really all about providing the proper type info to Dart. In the second example, Dart isn't properly inferring the List as a Middleware<AppState>
and therefore you need to help it out a bit.
Glad it's working!
And this is also work for me.
final wms = [persistor.createMiddleware()]..addAll(createMiddlewares());
if (TangboleConfig.debug) {
wms.add(LoggingMiddleware<AppState>.printer());
}
List<Middleware
Error
Code
Env