且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何替换已弃用的列表

更新时间:2023-02-19 14:17:10

根据官方文档:

@Deprecated(改用列表文字、[] 或 List.filled 构造函数")

注意:此构造函数不能在空安全代码中使用.使用 List.filled 创建一个非空列表.这需要一个填充值来初始化列表元素.要创建空列表,请对可增长列表使用 [] 或对固定长度列表使用 List.empty(或在运行时确定可增长性).

您可以改为这样做:

RosterToView.fromJson(Map json) {if (json['value'] != null) {rvRows = <RVRows>[];json['value'].forEach((v) {rvRows.add(new RVRows.fromJson(v));});}}

另一种选择是:

ListrvRows = [];

List has been deprecated. How do I re-write the following code?

  RosterToView.fromJson(Map<String, dynamic> json) {
    if (json['value'] != null) {
      rvRows = new List<RVRows>();
      json['value'].forEach((v) {
        rvRows.add(new RVRows.fromJson(v));
      });
    }
  }

According to the official documentation:

@Deprecated("Use a list literal, [], or the List.filled constructor instead")

NOTICE: This constructor cannot be used in null-safe code. Use List.filled to create a non-empty list. This requires a fill value to initialize the list elements with. To create an empty list, use [] for a growable list or List.empty for a fixed length list (or where growability is determined at run-time).

You can do this instead:

RosterToView.fromJson(Map<String, dynamic> json) {
    if (json['value'] != null) {
      rvRows = <RVRows>[];
      json['value'].forEach((v) {
        rvRows.add(new RVRows.fromJson(v));
      });
    }
  }

Another option is:

List<RVRows> rvRows = [];