我遇到一个错误,我无法弄清楚。我正在从操作中调用服务,并通过响应设置新的redux状态。但是,出现以下错误:
错误:
The argument type 'List<Chat> (C:\flutter\bin\cache\pkg\sky_engine\lib\core\list.dart)' can't be assigned to the parameter type 'List<Chat> (C:\flutter\bin\cache\pkg\sky_engine\lib\core\list.dart)'.
操作:
class GetChatRequest {}
class GetChatSuccess {
final List<Chat> history;
GetChatSuccess(this.history);
}
class GetChatFailure {
final String error;
GetChatFailure(this.error);
}
final Function getLastChatMessages = () {
return (Store<AppState> store) {
var chatService = new ChatService(store);
store.dispatch(new GetChatRequest());
chatService.getLast50Messages().then((history) {
store.dispatch(new GetChatSuccess(history));
});
};
};
服务:
Future<List<Chat>> getLast50Messages() async {
final response = await webClient.get('xxxx');
return response['data'].map<Chat>((e) => new Chat.fromJSON(e)).toList();
}
答案 0 :(得分:1)
更改
store.dispatch(new GetChatSuccess(history));
到
store.dispatch(new GetChatSuccess(List<Chat>.from(history)));
以获取正确键入的列表。
history
是一个List<dynamic>
,仅包含Chat
个元素,但是列表仍然具有通用类型dynamic
。要创建类型正确的List<Chat>
,请使用该类型创建一个新列表,并用history
中的元素填充它。
另请参阅https://api.dartlang.org/stable/2.1.0/dart-core/List/List.from.html