【问题标题】:Null check operator used on a null value error while using ! operator in flutter使用 ! 时用于空值错误的空值检查运算符颤振中的操作员
【发布时间】:2022-09-30 15:44:18
【问题描述】:

在我的项目中,我使用 api 请求访问了我的模型。但是当我运行它时,它会抛出“用于空值的空检查运算符”,我之前使用过相同的方法,它工作正常。我不知道这个发生了什么。

我的模型

class Client {
  List<Clients>? clients;

  Client({this.clients});

  Client.fromJson(Map<String, dynamic> json) {
    if (json[\'clients\'] != null) {
      clients = <Clients>[];
      json[\'clients\'].forEach((v) {
        clients!.add(new Clients.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.clients != null) {
      data[\'clients\'] = this.clients!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Clients {
  int? id;
  String? name;
  String? address;
  String? tinNo;
  String? status;
  String? createdAt;
  String? updatedAt;
  List<Phones>? phones;

  Clients(
      {this.id,
      this.name,
      this.address,
      this.tinNo,
      this.status,
      this.createdAt,
      this.updatedAt,
      this.phones});

  Clients.fromJson(Map<String, dynamic> json) {
    id = json[\'id\'];
    name = json[\'name\'];
    address = json[\'address\'];
    tinNo = json[\'tin_no\'];
    status = json[\'status\'];
    createdAt = json[\'created_at\'];
    updatedAt = json[\'updated_at\'];
    if (json[\'phones\'] != null) {
      phones = <Phones>[];
      json[\'phones\'].forEach((v) {
        phones!.add(new Phones.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data[\'id\'] = this.id;
    data[\'name\'] = this.name;
    data[\'address\'] = this.address;
    data[\'tin_no\'] = this.tinNo;
    data[\'status\'] = this.status;
    data[\'created_at\'] = this.createdAt;
    data[\'updated_at\'] = this.updatedAt;
    if (this.phones != null) {
      data[\'phones\'] = this.phones!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Phones {
  int? id;
  String? clientId;
  Null? supplierId;
  Null? companyId;
  String? phoneNumber;
  String? email;
  String? model;
  String? category;
  String? createdAt;
  String? updatedAt;

  Phones(
      {this.id,
      this.clientId,
      this.supplierId,
      this.companyId,
      this.phoneNumber,
      this.email,
      this.model,
      this.category,
      this.createdAt,
      this.updatedAt});

  Phones.fromJson(Map<String, dynamic> json) {
    id = json[\'id\'];
    clientId = json[\'client_id\'];
    supplierId = json[\'supplier_id\'];
    companyId = json[\'company_id\'];
    phoneNumber = json[\'phone_number\'];
    email = json[\'email\'];
    model = json[\'model\'];
    category = json[\'category\'];
    createdAt = json[\'created_at\'];
    updatedAt = json[\'updated_at\'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data[\'id\'] = this.id;
    data[\'client_id\'] = this.clientId;
    data[\'supplier_id\'] = this.supplierId;
    data[\'company_id\'] = this.companyId;
    data[\'phone_number\'] = this.phoneNumber;
    data[\'email\'] = this.email;
    data[\'model\'] = this.model;
    data[\'category\'] = this.category;
    data[\'created_at\'] = this.createdAt;
    data[\'updated_at\'] = this.updatedAt;
    return data;
  }
}

对于我的项目,我正在使用分页数据表并访问这些数据。代码如下。我在我的数据表源中将数据用作datasList[0].clients![index].address.toString(),。并将该数据传递为

DataTableSource dataSource(List<Client> userList) =>
      MyTable(datasList: userList, context: context);

数据源

source: dataSource(
                                  clientController.foundClients.value)

错误是当我使用 ! 运算符时。那么我该如何正确使用它呢?

  • 这个datasList[0].clients![index].address.toString()sn-p 在哪里使用。
  • 请添加更多代码以引用此datasList[0].clients![index].address.toString()

标签: flutter dart


【解决方案1】:

试试下面的代码:

class Client {
  List<Clients>? clients;

  Client({this.clients});

  Client.fromJson(Map<String, dynamic> json) {
    if (json['clients'] != null) {
      clients = <Clients>[];
      json['clients'].forEach((v) {
        clients?.add(new Clients.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.clients != null) {
      data['clients'] = this.clients?.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Clients {
  int? id;
  String? name;
  String? address;
  String? tinNo;
  String? status;
  String? createdAt;
  String? updatedAt;
  List<Phones>? phones;

  Clients(
      {this.id,
      this.name,
      this.address,
      this.tinNo,
      this.status,
      this.createdAt,
      this.updatedAt,
      this.phones});

  Clients.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    address = json['address'];
    tinNo = json['tin_no'];
    status = json['status'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    if (json['phones'] != null) {
      phones = <Phones>[];
      json['phones'].forEach((v) {
        phones?.add(new Phones.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['address'] = this.address;
    data['tin_no'] = this.tinNo;
    data['status'] = this.status;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    if (this.phones != null) {
      data['phones'] = this.phones?.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Phones {
  int? id;
  String? clientId;
  Null? supplierId;
  Null? companyId;
  String? phoneNumber;
  String? email;
  String? model;
  String? category;
  String? createdAt;
  String? updatedAt;

  Phones(
      {this.id,
      this.clientId,
      this.supplierId,
      this.companyId,
      this.phoneNumber,
      this.email,
      this.model,
      this.category,
      this.createdAt,
      this.updatedAt});

  Phones.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    clientId = json['client_id'];
    supplierId = json['supplier_id'];
    companyId = json['company_id'];
    phoneNumber = json['phone_number'];
    email = json['email'];
    model = json['model'];
    category = json['category'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['client_id'] = this.clientId;
    data['supplier_id'] = this.supplierId;
    data['company_id'] = this.companyId;
    data['phone_number'] = this.phoneNumber;
    data['email'] = this.email;
    data['model'] = this.model;
    data['category'] = this.category;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2021-07-08
    • 2021-11-17
    • 1970-01-01
    • 2021-09-25
    • 2022-07-20
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多