你可以使用 List.generate 根据特定条件赋予值吗?

huangapple go评论55阅读模式
英文:

Can I give values based on certain conditions using List.generate?

问题

我正在使用List.generate生成一个地图列表(List<Map<dynamic, dynamic>>)。我想在每个地图中放入一个键"Type",并根据列表中地图的索引分配一个值"A"、"B"或"C"中的一个值,具体取决于地图在列表中的索引(例如,索引范围从0到3的地图获取"Type": "A"等)。是否可以使用List.generate来实现这样的操作?

final List<Map<dynamic, dynamic>> flashcards = List.generate(10, (index) => {
  "isSelected": false,
  "id": index,
  "front": "front $index",
  "back": "back $index",
  "reviewDate": DateTime.now(),
  "answerStreak": 0,
  "Type": index < 3 ? "A" : (index < 6 ? "B" : "C"),
});
英文:

I'm generating a list of maps (List<Map<dynamic, dynamic>>) using List.generate. I wanted to put a key "Type" in each of the maps and assign it one of 3 values "A", "B" or "C" depending on the index of the map inside of the list (for example a map that has the index from the range 0 to 3 gets "Type": "A" and so on). Is there a way you can do such a thing using List.generate?

final List&lt;Map&lt;dynamic, dynamic&gt;&gt; flashcards = List.generate(10, (index) =&gt; {
  &quot;isSelected&quot;: false,
  &quot;id&quot;: index,
  &quot;front&quot;: &quot;front $index&quot;,
  &quot;back&quot;: &quot;back $index&quot;,
  &quot;reviewDate&quot;: DateTime.now(),
  &quot;answerStreak&quot;: 0,
  });

答案1

得分: 1

Sure, here is the translated code:

如果你打开List.generate中的回调函数,你可以执行多行操作(通过删除箭头函数声明),并且可以执行任意数量的if语句。例如下面的示例,扩展了num类以包括两种介于函数,以使if语句更加清晰

final List<Map<dynamic, dynamic>> flashcards = List.generate(10, (index) {
    if (index.isBetweenInclusive(0, 3)) {
        return {
            "isSelected": false,
            "id": index,
            "front": "front $index",
            "back": "back $index",
            "reviewDate": DateTime.now(),
            "answerStreak": 0,
        };
    } else if (index.isBetweenInclusive(4, 7)) {
        return {
            "isSelected": false,
            "id": index,
            "front": "front $index",
            "back": "back $index",
            "reviewDate": DateTime.now(),
            "answerStreak": 0,
        };
    } else {
        return {
            "isSelected": false,
            "id": index,
            "front": "front $index",
            "back": "back $index",
            "reviewDate": DateTime.now(),
            "answerStreak": 0,
        };
    }
});

extension Range on num {
  bool isBetween(num from, num to) {
    return from < this && this < to;
  }
}

extension Range on num {
  bool isBetweenInclusive(num from, num to) {
    return from <= this && this <= to;
  }
}

I've translated the code for you, excluding the code-related comments.

英文:

if you open up the callback function in List.generate you can perform multiline actions (by removing the arrow function declaration) and you can perform as many if statements you want. for example below, extended the num class to include 2 types of in between functions to make the if statements a bit cleaner

final List&lt;Map&lt;dynamic, dynamic&gt;&gt; flashcards = List.generate(10, (index) {
    if (index.isBetweenInclusive(0, 3)) {
        return {
            &quot;isSelected&quot;: false,
            &quot;id&quot;: index,
            &quot;front&quot;: &quot;front $index&quot;,
            &quot;back&quot;: &quot;back $index&quot;,
            &quot;reviewDate&quot;: DateTime.now(),
            &quot;answerStreak&quot;: 0,
        };
    } else if (index.isBetweenInclusive(4, 7)) {
        return {
            &quot;isSelected&quot;: false,
            &quot;id&quot;: index,
            &quot;front&quot;: &quot;front $index&quot;,
            &quot;back&quot;: &quot;back $index&quot;,
            &quot;reviewDate&quot;: DateTime.now(),
            &quot;answerStreak&quot;: 0,
        };
    } else {
        return {
            &quot;isSelected&quot;: false,
            &quot;id&quot;: index,
            &quot;front&quot;: &quot;front $index&quot;,
            &quot;back&quot;: &quot;back $index&quot;,
            &quot;reviewDate&quot;: DateTime.now(),
            &quot;answerStreak&quot;: 0,
        };
    }
});

extension Range on num {
  bool isBetween(num from, num to) {
    return from &lt; this &amp;&amp; this &lt; to;
  }
}

extension Range on num {
  bool isBetweenInclusive(num from, num to) {
    return from &lt;= this &amp;&amp; this &lt;= to;
  }
}

答案2

得分: 0

const abc = ["A", "B", "C"];
void main() {
  final List<Map<dynamic, dynamic>> flashcards = List.generate(
      10,
      (index) => {
            "isSelected": false,
            "id": index,
            "front": "front $index",
            "back": "back $index",
            "reviewDate": DateTime.now(),
            "answerStreak": 0,
            "Type": abc[index ~/ 4]
          });
}
英文:

Try this

const abc = [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;];
void main() {
  final List&lt;Map&lt;dynamic, dynamic&gt;&gt; flashcards = List.generate(
      10,
      (index) =&gt; {
            &quot;isSelected&quot;: false,
            &quot;id&quot;: index,
            &quot;front&quot;: &quot;front $index&quot;,
            &quot;back&quot;: &quot;back $index&quot;,
            &quot;reviewDate&quot;: DateTime.now(),
            &quot;answerStreak&quot;: 0,
            &quot;Type&quot;: abc[index ~/ 4]
          });
}

huangapple
  • 本文由 发表于 2023年6月5日 00:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定