如何在Dart中创建一个空列表
问题描述:
我想为Flutter项目创建一个空列表.
I want to create an empty list for a Flutter project.
final prefs = await SharedPreferences.getInstance();
final myStringList = prefs.getStringList('my_string_list_key') ?? <empty list>;
我似乎记得有多种方法,但有一种推荐的方法.我该怎么办?
I seem to remember there are multiple ways but one recommended way. How do I do it?
答
有几种方法可以在Dart中创建空列表:
There are a few ways to create an empty list in Dart:
[]
List()
<String>[]
List<String>()
但是,有效《 Dart使用指南》 建议这样做:
[]
或者如果您需要指定类型,则此:
Or if you need to specify the type, then this:
<String>[]