Home > Mobile >  List.add(x) function replacing all values in the list with x
List.add(x) function replacing all values in the list with x

Time:04-06

I am pretty new to flutter and I cannot figure out the issue in this code here:

List<ToolSteps> toolStepList = List<ToolSteps>.empty(growable: true);
print('my drawStep: ${drawStep.toJsonPencil()}'); // the toJsonPencil is just to print
toolStepList.add(ToolSteps(step: drawStep, toolName: 'test'));
toolStepList.forEach((element) {print(element.toJson());});

So the print functions return the same element multiple times, as if my .add is not adding at the end of the list but replacing the whole list with the value i am giving it. Here are some prints if that helps:

The first time I call the add function:

I/flutter ( 4463): my drawStep: {path: [{x: 153, y: 454}], strokeWidth: 20, colorInfo: {colorPrimary: f44336, colorSecondary: ffffff, opacityPrimary: ff, opacitySecondary: ff}}
I/flutter ( 4463): {toolName: pencil, step: {path: [{x: 153, y: 454}], strokeWidth: 20, colorInfo: {colorPrimary: f44336, colorSecondary: ffffff, opacityPrimary: ff, opacitySecondary: ff}}}

the second time I call the add function:

I/flutter ( 4463): my drawStep: {path: [{x: 868, y: 436}], strokeWidth: 20, colorInfo: {colorPrimary: f44336, colorSecondary: ffffff, opacityPrimary: ff, opacitySecondary: ff}}
I/flutter ( 4463): {toolName: pencil, step: {path: [{x: 868, y: 436}], strokeWidth: 20, colorInfo: {colorPrimary: f44336, colorSecondary: ffffff, opacityPrimary: ff, opacitySecondary: ff}}}
I/flutter ( 4463): {toolName: pencil, step: {path: [{x: 868, y: 436}], strokeWidth: 20, colorInfo: {colorPrimary: f44336, colorSecondary: ffffff, opacityPrimary: ff, opacitySecondary: ff}}}

You notice that my first value where the path was x:153 and y:454 got replaced by the new value. I cannot figure out why it does that. And these are the only places where modify the toolStepList. Would really appreciate help or clues.

I tried to create another list of Integers just in case and the same calls work, it just happens when the Type I pass to my list is of ToolSteps in this case

The function to add to the list is called in a gesture detector onPanEnd method:

onPanEnd(DragEndDetails details) {
print('my drawStep: ${drawStep.toJsonPencil()}'); // the toJsonPencil is just to print
toolStepList.add(ToolSteps(step: drawStep, toolName: 'pencil'));
toolStepList.forEach((element) {print(element.toJson());});

}

CodePudding user response:

Your ToolSteps step attribute value is a reference to drawStep, so if you change drawsStep it will change in all places where it's used. Instead of passing in the same drawStep variable you should initialize your Step inside inside the ToolSteps constructor like this:

onPanEnd(DragEndDetails details) {
toolStepList.add(ToolSteps(step: Step(path: x,y from details, strokewidth: value from details, colorinfo: value from details, etc...), toolName: 'test'));
}

or create a new Step variable every time the gesture detector method is called, like this

onPanEnd(DragEndDetails details) {
var newDrawStep= -whatever you fetch from details-
toolStepList.add(ToolSteps(step: newDrawStep, toolName: 'pencil'));
}
  • Related