如何替换不推荐使用的列表 [英] How to replace deprecated List

查看:53
本文介绍了如何替换不推荐使用的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表已被弃用.如何重新编写以下代码?

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));
      });
    }
  }

推荐答案

根据官方文档:

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

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

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

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).

您可以改为这样做:

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

另一个选择是:

List<RVRows> rvRows = [];

这篇关于如何替换不推荐使用的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆