recently I was working with Redux state management in Flutter and got this issue The middleware isn't able to trigger dispatch here is the snippet for the code you need to analyse the problem
Action:
class GetLeaveType {
GetLeaveType();
}
class LoadLeaveType {
List<GetLeaveTypeResponse> getLeaveTypeResponseList;
LoadLeaveType({
this.getLeaveTypeResponseList,
});
}
Reducer (BTW LoadShowAndHideRequests action work in this reducer)
final newRequestReducer = combineReducers<NewRequestModel>([
new TypedReducer<NewRequestModel, LoadTypes>(loadTypes),
new TypedReducer<NewRequestModel, LoadShowAndHideRequests>(loadShowAndHideRequests),
new TypedReducer<NewRequestModel, LoadLeaveType>(loadLeaveType),
]);
NewRequestModel loadLeaveType(NewRequestModel state, LoadLeaveType action) {
return state.copyWith(
getLeaveTypeResponseList: action.getLeaveTypeResponseList,
leaveReturnRequest: state.leaveReturnRequest.copyWith(
recId: action.getLeaveTypeResponseList != null && action.getLeaveTypeResponseList.length > 0
? action.getLeaveTypeResponseList.elementAt(0).id
: "",
),
);
}
Middleware:
The method
void middlewareGetLeaveType(
Store<AppState> store,
GetLeaveType action,
NextDispatcher next,
) async {
await RequestsService.getLeaveType().then(
(value) {
return store.dispatch(
LoadLeaveType(
getLeaveTypeResponseList: value,
),
);
},
);
}
The combined:
List<Middleware<AppState>> createAllMiddleware() {
return [
//region Reports
TypedMiddleware<AppState, GetPayslipData>(middlewareGetPayslipData),
TypedMiddleware<AppState, GetPayslipFile>(middlewareGetPayslipFile),
//endregion
//region Requests
TypedMiddleware<AppState, GetLeaveType>(middlewareGetLeaveType),
//endregion
];
}
I would be grateful for any suggestion Thanks in advance.