list - Flutter/Dart:如何从一个方法返回一个列表并将其设置为另一个列表
问题描述
问题:如何从列表中检索并附加到本地保存的新列表中,以供同一类中的方法使用。
我想将whats存储在exerciseList中并将其放入局部变量List CustomExercises中;当我从 getAllExercisesAsStrings() 返回自定义练习时,它不会更新。
class GenerateCustom extends ExerciseListState {
int rnd;
GenerateCustom({this.difficulty});
final int difficulty;
String workout;
String ex1;
String ex2;
String ex3;
String ex4;
String ex5;
List customExercises = [];
//get list of custom workouts
List getAllExercisesAsStrings(customExercises) {
var n;
for (n = 0; n < exerciseList.length; n++) {
// print(exerciseList[n].title);
customExercises.add(exerciseList[n].title);
}
return customExercises;
}
对于上下文,getCustomType() 从要显示的列表中获取随机练习。
String getCustomType() {
var random = Random();
var i = random.nextInt(customExercises.length);
print(customExercises[i]);
return customExercises[i];
}
String cExerciseOne() {
if (difficulty == 1) {
workout =
('1: ' + getCustomType() + ' ' + getRepsEasy() + 'x' + getSetsEasy());
} else if (difficulty == 2) {
workout = ('1: ' +
getCustomType() +
' ' +
getRepsMedium() +
'x' +
getSetsMedium());
} else {
workout =
('1: ' + getCustomType() + ' ' + getRepsHard() + 'x' + getSetsHard());
}
return workout;
}
当我打印到控制台exerciseList[n].title
时,它会返回带有练习['exercise1','Bicep Curl', 'Pull Up', ]
等的列表。这些练习已经在父类中检索到,我想将它们存储在新列表中。任何指导都会很棒,如果您认为需要更多背景信息,请告诉我。
解决方案
推荐阅读
- oracle - 发布日期从 1-jan-xx 年至今
- c# - 使用 Newtonsoft 反序列化 JSON,并在值中使用引号
- docker - 是否可以在 2 个 docker-compose.yaml 之间通过 docker-compose 在 docker 网络上卷曲?
- windows - git-bash:'。' 未被识别为内部或外部命令、可运行程序或批处理文件
- spring - 我正在尝试使用 Spring JPA 存储库中的 userName 获取用户数据,但它总是返回一个空列表
- javascript - 如何使用汇总来捆绑包含 es7 代码的文件?
- sql - 根据所选月份的范围,使用一条 SQL 语句插入多行
- android - Android Webview 应用程序,无法使用图像上传按钮,点击选择图像时没有任何反应
- c++ - 是 *this = Tor(); 清除对象状态是否合法有效?
- android - 如何轻松检索我的全部或部分外部 IP 地址(在 Android 设备上)?