首页 > 解决方案 > 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', ]等的列表。这些练习已经在父类中检索到,我想将它们存储在新列表中。任何指导都会很棒,如果您认为需要更多背景信息,请告诉我。

标签: listflutterdartsqflite

解决方案


推荐阅读