如何确定Dart列表是否为固定列表? [英] How to determine if a Dart list is a fixed list?

查看:68
本文介绍了如何确定Dart列表是否为固定列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时确定Dart中的列表是否为固定列表?

How can I determine, at runtime, if a list in Dart is a "fixed list" ?

(至少)有三种创建固定值的方法Dart中的长度列表:

There are (at least) three ways to create a fixed-length list in Dart:

var fixed = new List(5); // fixed at five elements
var alsoFixed = new List.filled(5, null); // fixed at five elements, set to null
var fixedToo = new List.from([1,2,3], growable: false);

如何在代码中询问是否固定了 还固定 fixedToo 是固定长度的吗?

How do I ask, in code, if fixed, alsoFixed, and fixedToo are fixed-length?

推荐答案

您可以尝试添加一个元素并将其删除,以了解列表是否具有固定长度:

You can try to add an element and remove it to know if the list has a fixed length:

bool hasFixLength(List list) {
  try {
    list
      ..add(null)
      ..removeLast();
    return false;
  } on UnsupportedError {
    return true;
  }
}

这篇关于如何确定Dart列表是否为固定列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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