skip to Main Content

I have a list of Workout class and I want to delete a specific element from the list

   class Exercise {
       String name;
       String weight;
       String reps;
       String sets;
      bool isCompleted;
    
      Exercise({
        required this.name,
        required this.weight,
        required this.reps,
        required this.sets,
        this.isCompleted = false,
      });
      void setName(String name){
        this.name=name;
      }
       String getName(){
         return this.name;
       }
       void setWeight(String weight){
         this.weight=weight;
       }
       void setReps(String reps){
         this.reps=reps;
       }
       void setSets(String sets){
         this.sets=sets;
       }
    }


 
    import 'package:fitness_tracker/models/exercise.dart';
        
    class Workout {
          String name;
    
          final List<Exercise> exercises;
          Workout({required this.name, required this.exercises});
        
          void setWorkoutName(String name) {
            this.name=name;
          }
          String getWorkoutName(){
            return this.name;
          }
        }
void deleteOneWorkout(String workoutName, int index)async {
    int count = 0;
    for (int i = 0; i < workoutList.length; i++) {
      Workout workout = workoutList[i];
      if (workout.getWorkoutName()==workoutName) {
        if (count == index) {
          print(index);
          workoutList.removeAt(i);
          notifyListeners();
          // Save to database
          db.saveToDatabase(workoutList);
          // Load heat map
          loadHeapMap();
        }
        count++;
      }
    }
  }

I tried to print the length of the list and it’s still the same length why removeAt is not working?
I add a print statement right before the removeAt line (in the inner if block) to see if the execution actually reached there

if I have in the list "we" at index 0 and "go" at index 1 and "we" at index 2 and if I try to remove the "we" at index 2 nothing is printed if I try to remove "we" at index 0 it is print the i value and remove the element

Thank you all

The problem is solved

3

Answers


  1. Try using break.

      void deleteOneWorkout(String workoutName, int index) async {
          int count = 0;
          for (int i = workoutList.length - 1; i >= 0; i--) {
            Workout workout = workoutList[i];
            if (workout.getWorkoutName() == workoutName) {
              if (count == index) {
                print(index);
                workoutList.removeAt(i);
                notifyListeners();
                // Save to database
                db.saveToDatabase(workoutList);
                // Load heat map
                loadHeapMap();
                break; // Exit the loop after removing the element
              }
              count++;
            }
          }
        }
    
    Login or Signup to reply.
  2. I am not completely understand about your logic code. Why you pass index to deleteOneWorkout function and what is this purpose of count flag?.

    But if you want to remove object that matched by workoutName, You can write like that.

    void deleteOneWorkout(String workoutName, int index)async {
      for (int i = 0; i < workoutList.length; i++) {
        Workout workout = workoutList[i];
        if (workout.getWorkoutName() == workoutName && index == i) {
            workoutList.removeAt(i);
        }
      }
      print("List ${workoutList.length}");
    }
    
    Login or Signup to reply.
  3. In this code, the deleteExercise function takes the name of the exercise that you want to delete. It then uses the removeWhere method of the list to remove the exercise whose name matches the provided name. This will solve your problem

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search