【问题标题】:Flutter Unhandled Exception: type 'Null' is not a subtype of type 'List<dynamic>' in type castFlutter 未处理的异常:类型“Null”不是类型转换中“List<dynamic>”类型的子类型
【发布时间】:2022-01-13 04:00:01
【问题描述】:

我需要将模型分配给应用程序中的列表,但出现标题中提到的错误。

虽然我在android端得到这个错误,但我没有问题,但是当我在ios端尝试时,我的应用程序崩溃了。

List<MessageModel> messageList = [];
String? message;
bool success = false;

@override
MessageService decode(dynamic data) {
messageList = (data as List).map((e) => MessageModel.fromJsonData(e)).toList(); ----> Unhandled Exception: type 'Null' is not a subtype of type 'List<dynamic>' in type cast
return this;
}

【问题讨论】:

    标签: ios list flutter casting decode


    【解决方案1】:

    您收到错误的原因是data 为空。

    试试我的解决方案:

    List<MessageModel> messageList = [];
    String? message;
    bool success = false;
    
    @override
    MessageService decode(dynamic data) {
      messageList = data?.map((e) => MessageModel.fromJsonData(e))?.toList() ?? [];
      return this;
    }
    

    【讨论】:

    • 现在我的列表不再得到并得到 [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:类型“_TypeError”不是类型中“DioError”类型的子类型投射错误
    • 你的data是什么类型的?
    【解决方案2】:
        List<MessageModel> ?messageList = [];
    String? message;
    bool success = false;
    
    @override
    MessageService decode(dynamic data) {
    messageList = (data as List).map((e) => MessageModel.fromJsonData(e)).toList(); ----> Unhandled Exception: type 'Null' is not a subtype of type 'List<dynamic>' in type cast
    return this;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-28
      • 2021-08-15
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多