【问题标题】:I can t use this list and i have this problem我不能使用这个列表,我有这个问题
【发布时间】:2021-11-24 13:09:54
【问题描述】:

我对这个列表有疑问

错误说:启用空安全时,默认的“列表”构造函数不可用

class Geometry {
      late List<List> coordinates  ;
      late String type;
    
      Geometry({required this.coordinates, required this.type});
    
      Geometry.fromJson(Map<String, dynamic> json) {
        if (json['coordinates'] != null) {
          coordinates = new List<List>();
          json['coordinates'].forEach((v) { coordinates.add(new List.fromJson(v)); });
        }
        type = json['type'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.coordinates != null) {
          data['coordinates'] = this.coordinates.map((v) => v.toJson()).toList();
        }
        data['type'] = this.type;
        return data;
      }
    }

【问题讨论】:

标签: flutter


【解决方案1】:

而不是前空安全操作

var foo = List<int>();  // Now error
var bar = List<int>(n); // Now error
var baz = List<int>(0); // Now error

使用以下内容:

var foo = <int>[];           // Always the recommended way.
var bar = List.filled(1, 0); // Not filled with `null`s.
var baz = List<int>.empty();

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
相关资源
最近更新 更多