Home > Software engineering >  List.add not executing and returning the calling function
List.add not executing and returning the calling function

Time:04-25

I have a simple problem and I think I am missing something. I have a class called CanvasState which is just a data class holding two List<Offset?> variables and the class CanvasHistory which holds a List of CanvasState. Now when I am calling the addState method of CanvasHistory to add a CanvasState to the statesList variable, the List.add function is either not executing or it just returns the calling function. I commented the code to be more precise.

import 'package:flutter/material.dart';

class CanvasHistory {
  List<CanvasState> statesList = <CanvasState>[];
  late int index = 0;

  CanvasHistory({this.statesList = const <CanvasState>[]}) {
    int length = statesList.length;
    index = length - 1;
  }

  void addState(CanvasState state) {
    print("adding state \n"   state.toString()); // This print statement is printed
    statesList.add(state); // somehow this code is not executed or returns
    print("AFTER"); // This print statement is not executed.
  }
}

class CanvasState {
  final List<Offset?> _points;
  final List<Offset?> _markerPoints;

  CanvasState(this._points, this._markerPoints);

  List<Offset?> get points {
    return _points;
  }

  List<Offset?> get markerPoints {
    return _markerPoints;
  }
}

I have no Idea what is wrong here. I am not using asynchronous functions and it is really weird, that the second print statement is not executed.

Any help would be appreciated, thanks!

CodePudding user response:

Ok, so here is the problem:

You have a default value for your list of const <CanvasState>[], The problem is that you can't add items to a const array. So when you try, the method throws an error and returns (if you look at your debug console, you should hopefully see an error like "add operation not supported").

To fix this, you simply have to make the default value non-constant. But oh-oh! What's this?

The default value of an optional parameter must be constant.

Dart complains again. Here is what I recommend:

CanvasHistory({List<CanvasState>? statesList}) {
  if (statesList != null) {
    this.statesList = statesList;
  }
  int length = statesList.length;
  index = length - 1;
}

There probably is a cleaner solution, but this is the simplest one I could think of.

Also, as Abhishek Thulasi says, You don't need the late keyword on your index variable, you can remove it, although I don't think it is the cause of the bug.

CodePudding user response:

the problem is that a default value will create a const list which you can't add any item to it so you can't use add method without a error

a solution is to use a late keyword like

class CanvasHistory {
  late List<CanvasState> statesList ;
  late int index ;

  CanvasHistory({List<CanvasState>? statesList}) {
  this.statesList = statesList??[];
  final length = this.statesList.length;
  index = length - 1;
}
}
  • Related