首页 > 解决方案 > 仅当在 if 语句颤动中返回多个时才限制为 1

问题描述

假设我有一个给定的数组列表

Array listarray = ['1','2','3','3','3','4','5','6','6','7','8']

for (int i = 0; i<listarray.length; i++){
 if(listarray[i] == '3'){
   return 'this is three';
 }else if (listarray[i] == '6'){
   return 'this is six';
 }
}

它将返回为

this is threethis is threethis is threethis is sixthis is six

我想知道是否有办法让我只退回第一个或将其限制为仅退回 1 个?

这样它就会像

this id threethis is six

标签: arraysflutterdart

解决方案


首先:dart中没有Array类型,但有List

您可以通过将列表转换为设置来删除重复项:

var listarray = ['1','2','3','3','3','4','5','6','6','7','8'];
listarray = listarray.toSet().toList();

推荐阅读