英文:
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<Map<dynamic, dynamic>> flashcards = List.generate(10, (index) => {
"isSelected": false,
"id": index,
"front": "front $index",
"back": "back $index",
"reviewDate": DateTime.now(),
"answerStreak": 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<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;
}
}
答案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 = ["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]
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论