Flutter:不推荐使用列表? [英] Flutter: List is deprecated?
本文介绍了Flutter:不推荐使用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
升级到最新版本的 flutter 后,我的所有列表都收到了弃用警告.
After upgrading to the latest version of flutter, I get a deprecation warning for all my Lists.
List<MyClass> _files = List<MyClass>();
=>'List' is deprecated and shouldn't be used.
不幸的是,它没有提示用什么来替换它.那么我们现在应该用什么来代替?
Unfortunately, it does not give a hint of what to replace it with. So what are we supposed to use instead now?
- Dart SDK 版本:2.12.0-141.0.dev
- Flutter:频道大师,1.25.0-9.0.pre.42
推荐答案
好的找到了,就是如何实例化了:
Ok found it, it's just how to instantiate it:
List<MyClass> _files = [];
编辑:可能是最常见的,根据 文档:
Edit: maybe the most common ones, a bit more detailed according to the docs:
大小为 0 的定长列表
Fixed-length list of size 0
List<MyClass> _list = List<MyClass>.empty();
可增长的列表
List<MyClass> _list = [];
//or
List<MyClass> _list = List<MyClass>.empty(growable: true);
具有预定义填充的固定长度
Fixed length with predefined fill
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']
这篇关于Flutter:不推荐使用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文