Flutter:InternalLinkedHashMap'没有具有匹配参数的实例方法'cast'

Posted

技术标签:

【中文标题】Flutter:InternalLinkedHashMap\'没有具有匹配参数的实例方法\'cast\'【英文标题】:Flutter: InternalLinkedHashMap' has no instance method 'cast' with matching argumentsFlutter:InternalLinkedHashMap'没有具有匹配参数的实例方法'cast' 【发布时间】:2020-04-16 14:54:58 【问题描述】:

这是 JSON 响应,我需要将数据检索到颤振中。 投射细节出现错误。

code": 200,
    "message": "Success",
    "data": [
        

           "fname": "LG",
            "lname": "Electronics",
            "email": "lg@gmail.com",
            "address": "Girish Cold Drinks, Chimanlal Girdharlal Road, Mithakhali, Navrangpura, Ahmedabad, Gujarat, India",

        
    

这里是客户类

class Customer 
  int code;
  String message;
  List<Data> data;
  Customer(this.code, this.message, this.data);
  Customer.fromJson(Map<String, dynamic> json) 
    code = json['code'];
    message = json['message'];
    if (json['data'] != null) 
      data = new List<Data>();
      json['data'].forEach((v) 
        data.add(new Data.fromJson(v));
      );
    
  
  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['code'] = this.code;
    data['message'] = this.message;
    if (this.data != null) 
      data['data'] = this.data.map((v) => v.toJson()).toList();
    
    return data;
  




  class Data 
      String fname;
      String lname;
      String email;
      String address;

    Data(
          
            this.fname,
            this.lname,
            this.email,
            this.address,
            );





 Data.fromJson(Map<String, dynamic> json) 
        fname = json['fname'];
        lname = json['lname'];
        email = json['email'];
        address = json['address'];
      

   Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();

    data['fname'] = this.fname;
    data['lname'] = this.lname;
    data['email'] = this.email;
    data['address'] = this.address;

    return data;
  

这里我遇到了投射错误

final jsonResponse = json.decode(response.body).cast<Map<String, dynamic>>();
      List<Customer> listOfUsers = jsonResponse.map<Customer>((json) async 
        return  Customer.fromJson(json);
      ).toList()

错误:Flutter:InternalLinkedHashMap'没有实例方法'cast'和匹配的参数

【问题讨论】:

【参考方案1】:

您可以使用Customer.fromJson(json.decode(response.body))

对于任何复杂的 JSON 解析,请使用 quicktype.io。

更新: 你的 pojo 类将是这样的:

import 'dart:convert';
Customer customerFromJson(String str) => Customer.fromJson(json.decode(str));
String customerToJson(Customer data) => json.encode(data.toJson());
class Customer 
    int code;
    String message;
    List<Data> data;
    Customer(
        this.code,
        this.message,
        this.data,
    );
    factory Customer.fromJson(Map<String, dynamic> json) => Customer(
        code: json["code"],
        message: json["message"],
        data: List<Data>.from(json["data"].map((x) => Data.fromJson(x))),
    );
    Map<String, dynamic> toJson() => 
        "code": code,
        "message": message,
        "data": List<dynamic>.from(data.map((x) => x.toJson())),
    ;

class Data 
    String fname;
    String lname;
    String email;
    String address;
    Data(
        this.fname,
        this.lname,
        this.email,
        this.address,
    );
    factory Data.fromJson(Map<String, dynamic> json) => Data(
        fname: json["fname"],
        lname: json["lname"],
        email: json["email"],
        address: json["address"],
    );
    Map<String, dynamic> toJson() => 
        "fname": fname,
        "lname": lname,
        "email": email,
        "address": address,
    ;

【讨论】:

但在模型类中使用 this=> Customer.fromJson (Map json)。所以这个解决方案不起作用需要强制转换或任何其他解决方案 @KumarSunil17 不能将“客户”类型的值分配给“列表”类型的变量。出现错误。 我在从数据['data'] 中获取列表时遇到问题。它给出了一个转换错误。【参考方案2】:

Customer 中试试这个来关注,

   class Customer 
      int code;
      String message;
      List<Data> data;
      Customer(this.code, this.message, this.data);
      factory  Customer.fromJson(Map<String, dynamic> json) 
        code = json['code'];
        message = json['message'];
         var list = json['data'] as List;
        List<Data> dataList =
            list != null ? list.map((i) => Data.fromJson(i)).toList() : [];

      retrun Customer(code:code,message,data:dataList)
      


      Map<String, dynamic> toJson() 
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['code'] = this.code;
        data['message'] = this.message;
        if (this.data != null) 
          data['data'] = this.data;
        
        return data;
      

Data正在关注

class Data 
      String fname;
      String lname;
      String email;
      String address;

    Data(
           this.fname,
            this.lname,
            this.email,
            this.address,
            );

 factory Data.fromJson(Map<String, dynamic> json) 
        fname = json['fname'];
        lname = json['lname'];
        email = json['email'];
        address = json['address'];
        return Data(fname:fname,lname:lname,email:email,address:address);
      

   Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();

    data['fname'] = this.fname;
    data['lname'] = this.lname;
    data['email'] = this.email;
    data['address'] = this.address;

    return data;
  

【讨论】:

我在从数据['data'] 中获取列表时遇到问题。它给出了一个转换错误。 @RahulDevanavar 这个错误花花公子提到的问题 Error :Flutter: InternalLinkedHashMap' has no instance method 'cast' with matching arguments

以上是关于Flutter:InternalLinkedHashMap'没有具有匹配参数的实例方法'cast'的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 致命错误:找不到“Flutter/Flutter.h”文件

[Flutter] flutter项目一直卡在 Running Gradle task 'assembleDebug'...

flutter 日志输出,Flutter打印日志,flutter log,flutter 真机日志

Flutter开发 Flutter 包和插件 ( Flutter 包和插件简介 | 创建 Flutter 插件 | 创建 Dart 包 )

flutter与原生混编(iOS)

Flutter-布局