开始着手Flutter的研究项目,我想知道如何排序文件列表。
实际上,我的程序有一个包含4个初始化文件的列表,如下所示:
List<File> imageFiles = List(4);
字符串
这个初始化实际上意味着我的列表是这样的:[null,null,null,null]
。
当用户执行操作时,这个列表可以填满。但是,用户可以随时删除文件,这可以给予我们以下情况:[file A, null, null, file d]
。
我的问题是,当一个删除到达时,如何对列表进行排序,以获得一个空对象总是最后的列表([file A, file D, null, null]
)。
我已经看了很多主题,但他们从来没有涉及DART。
提前感谢您的帮助。
3条答案
按热度按时间svmlkihl1#
您可以使用
list.sort((a, b) => a == null ? 1 : 0);
对列表进行排序下面是一个完整的示例,使用
String
而不是File
,您可以在DartPad上运行它字符串
如果业务要求最多有4个文件,我建议创建一个值对象来处理。例如:
型
你可以这样运行它:
型
这将使控制更容易。(这个例子再次使用
String
而不是File
)ipakzgxi2#
你可以试试这个:Demo (Dartpad)
这个地方最终都是空的。
字符串
你也可以使用这个演示:Demo 2 - Dartpad
bq9c1y663#
对于任何感兴趣的人,我对Jorge的答案做了一个扩展函数。
字符串