代码之家  ›  专栏  ›  技术社区  ›  Zander17

参数列表无法分配给参数类型列表

  •  1
  • Zander17  · 技术社区  · 7 年前

    我遇到了一个我搞不清楚的错误。我从操作中调用服务,并用响应设置新的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();
      }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Günter Zöchbauer    7 年前

    变化

    store.dispatch(new GetChatSuccess(history));
    

    store.dispatch(new GetChatSuccess(List<Chat>.from(history)));
    

    获取正确键入的列表。

    history 是一个 List<dynamic> 只包含 Chat 元素,但列表仍具有泛型类型 dynamic . 创建正确类型的 List<Chat> 用该类型创建一个新列表,并用其中的元素填充它 历史 .

    也见 https://api.dartlang.org/stable/2.1.0/dart-core/List/List.from.html