【问题标题】:Flutter: List is deprecated? [duplicate]Flutter:列表已弃用? [复制]
【发布时间】:2021-03-24 10:23:27
【问题描述】:

升级到最新版本的颤振后,我的所有列表都会收到弃用警告。

List<MyClass> _files = List<MyClass>();
=>'List' is deprecated and shouldn't be used.

不幸的是,它没有给出替换它的提示。 那么我们现在应该改用什么呢?

  • Dart SDK 版本:2.12.0-141.0.dev
  • Flutter:通道主机,1.25.0-9.0.pre.42

【问题讨论】:

标签: flutter dart


【解决方案1】:

好的,找到了,就是如何实例化它:

List<MyClass> _files = [];

编辑:可能是最常见的,根据docs更详细一点:

大小为0的定长列表

List<MyClass> _list = List<MyClass>.empty();

可增长列表

List<MyClass> _list = [];
//or
List<MyClass> _list = List<MyClass>.empty(growable: true);

预定义填充的固定长度

int length = 3;
String fill = "test";
List<String> _list =  List<String>.filled(length ,fill , growable: true);
// => ["test", "test", "test"]

带有生成功能的列表

int length = 3;
MyClass myFun(int idx) => MyClass(id: idx);
List<MyClass> _list = List.generate(length, myFun, growable: true); 
// => [Instance of 'MyClass', Instance of 'MyClass', Instance of 'MyClass']

【讨论】:

  • 如何启动这样的静态大小的列表? (它给出了相同的弃用警告)
  • @spydon 查看编辑
【解决方案2】:
List<MyClass> myList = <MyClass>[];

【讨论】:

    【解决方案3】:

    发件人:

    _todoList = new List();
    

    改为:

    _todoList = [];
    

    【讨论】:

      【解决方案4】:

      旧版本

       List<Widget> widgetList = new List<Widget>();
      

      新版本

      List<Widget> widgetList = [];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-25
        • 1970-01-01
        • 2021-01-31
        • 1970-01-01
        • 2021-06-17
        • 2020-04-20
        • 1970-01-01
        相关资源
        最近更新 更多