New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypedMiddleware not accept specified middleware type. #35
Comments
What if you try this? This is similar to your other problem: Dart is inferring the more specific type information that List<Middleware<AppState>> createMiddlewares({
AccountService accountService,
}) {
accountService = accountService ?? AccountService();
return <Middleware<AppState>>[
TypedMiddleware<AppState, LoginAction>(loginMiddleware(accountService)),
];
} |
Because I want use code assistant for action! Using type dynamic lost the exact action type information, and there is no code assistant. The action type param is already given to TypedMiddleware, so why not using the exact middleware type? |
I'd ask: Please let us be kind and patient with one another. I'm doing my best to help you with the very limited amount of free time I have. In this case, your app has a small type error. Could you please try this and see if it works? I realized the mistake is in a slightly different part of your code. Please change this: Middleware<AppState> loginMiddleware(AccountService accountService) =>
// Specify the exact type of action, not dynamic
(Store<AppState> store, LoginAction action, NextDispatcher next) {
next(action);
}; to this: void Function(
Store<State> store,
LoginAction action,
NextDispatcher next,
) loginMiddleware(AccountService accountService) =>
(Store<AppState> store, LoginAction action, NextDispatcher next) {
next(action);
}; In this case, you want to return a In this case, you need to conform to what the |
Sorry, I read this "What if you try this?" to "Why if you try this?":) Anyway, using the Middleware<AppState> loginMiddleware(AccountService accountService) =>
(Store<AppState> store, dynamic action, NextDispatcher next) {
final a = action as LoginAction;
accountService.login(a.username, a.password).then((response) {
if (response.code == TangboleApiResponseCode.ok) {
store.dispatch(
LoginSucceededAction(UserModel.fromJson(response.data['user'])));
} else {
store.dispatch(NotifyAction(NoticeModel(message: response.message)));
}
});
}; |
No problem at all. As a heads up: your current solution might lead to runtime errors. If the action is not a class MyMiddleware extends TypedMiddleware<AppState, LoginAction> {
MyMiddleware(AccountService service)
: super(
(
Store<AppState> store,
LoginAction action,
NextDispatcher next,
) {
service.login(a.username, a.password).then((response) {
if (response.code == 200) {
store.dispatch(LoginSucceededAction(
UserModel.fromJson(response.data['user'])));
} else {
store.dispatch(
NotifyAction(NoticeModel(message: response.message)));
}
});
},
);
} |
Since we solved the original problem, I'll go ahead and close this issue. Please let me know if ya need more help :) |
There is another way. List<Middleware<AppState>> createMiddlewares({
AccountService accountService,
}) {
accountService = accountService ?? AccountService();
return [
TypedMiddleware<AppState, LoginAction>(loginMiddleware(accountService)),
];
}
dynamic loginMiddleware(AccountService accountService) =>
(Store<AppState> store, LoginAction action, NextDispatcher next) {
accountService.login(action.username, action.password).then((response) {
if (response.code == TangboleApiResponseCode.ok) {
store.dispatch(
LoginSucceededAction(UserModel.fromJson(response.data['user'])));
} else {
store.dispatch(NotifyAction(NoticeModel(message: response.message)));
}
});
}; As Dart not support generic param extending? As type |
Sure, that works. I'd personally use the full type info rather than
We might need to look at changing this -- In Dart 1, |
Problem
When specify the exact action type, TypedMiddleware complains
[dart] The return type '(Store<AppState>, LoginAction, (dynamic) → void) → Null' isn't a '(Store<AppState>, dynamic, (dynamic) → void) → void', as defined by the method 'loginMiddleware'.
.Code
Env
The text was updated successfully, but these errors were encountered: