Home > Back-end >  Flutter generating list with FOR loop via GET method
Flutter generating list with FOR loop via GET method

Time:02-24

Good morning. I am building simple workout app in Flutter. I want to generate list of custom class Excersise:

class Excersise{
   int id;
   String name;
   int duration;
   int type;

  Excersise({
  required this.id,
  required this.duration,
  required this.name,
  required this.type,
});

I want to pass parameter rounds, wtime(workout time) and ctime(cooldown time) from parent widget and then my app should create list of excersises. It should be pairs of workout and cool down time like this: wo 30sec, cd 15sec, wo 30sec, cd 15sec and so one (based on rounds value). I tried to generate this list via GET but after building this widget i get Stack overflow error. Maybe there is some error in code, maybe this is completely wrong method. Here is a code and thank for your advices.

class QuickWorkoutList extends StatelessWidget {
  final int rounds;
  final int wtime;
  final int ctime;

  QuickWorkoutList({
    required this.rounds,
    required this.wtime,
    required this.ctime,
  });

  List<Excersise> get _qList {
    var newEx = Excersise(id: 0, duration: 0, name: '', type: 0);

    for (var i = 0; i < rounds * 2; i  ) {
      if (i.isEven) {
        newEx.id = i;
        newEx.name = 'Workout';
        newEx.type = 0;
        newEx.duration = wtime;
      } else {
        newEx.id = i;
        newEx.name = 'Cooldown';
        newEx.type = 1;
        newEx.duration = ctime;
      }
      _qList.add(newEx);
    }
        throw 'No data';
  }

CodePudding user response:

Your getter method _qList is wrong implemented. You should create an empty List<Excersise> and add the Workout and Cooldown there. Currently you are recursively calling your _qList which has no anchor to stop. Consequently you have an endless loop. Btw I think you mean Exercise instead of Excersise.

List<Exercise> get _qList {
    // All exercises stored
    final List<Exercise> exercises = [];
    
    // Exercise properties
    int duration;
    String name;
    int type;

    for (int i = 0; i < rounds * 2; i  ) {
      // Get exercise properties depending on id
      if (i.isEven) {
        name = 'Workout';
        type = 0;
        duration = wtime;
      } else {
        name = 'Cooldown';
        type = 1;
        duration = ctime;
      }

      // Add new exercise to list
      exercises.add(
        Excersise(
          id: i,
          duration: duration,
          name: name,
          type: type,
        ),
      );
    }
    return exercises;
  }

CodePudding user response:

You shouldn't call add on your _qList getter, and that creates stack overflow I guess. You should use new List and return it from your _qList getter like this:

    List<Excersise> get _qList {
    var newEx = Excersise(id: 0, duration: 0, name: '', type: 0);
    List<Excersise> exercises = [];

    for (var i = 0; i < rounds * 2; i  ) {
      if (i.isEven) {
        newEx.id = i;
        newEx.name = 'Workout';
        newEx.type = 0;
        newEx.duration = wtime;
      } else {
        newEx.id = i;
        newEx.name = 'Cooldown';
        newEx.type = 1;
        newEx.duration = ctime;
      }
      exercises.add(newEx);
    }
    return exercises;
  }
  • Related