The bug that always returns

Danielle H
2 min readJun 23, 2022

Somehow, I never get too experienced for this bug…

I have been programming for more than twenty years. And there is one bug that somehow I keep seeing, debugging, and then having the *facepalm* moment of “Oh no…, not this again…”

not again!

In this case it happened in Flutter. I created a widget that shows a user-defined number of plots. It accepts a GraphDataNotifier, which is my extension of ValueNotifier<List<List<Data>>, where Data is an object with double time and double value.

In other words, it is a ValueNotifier that holds a List of List of data. Each sub-list is one plot. So if the list has only one sub-list, then the graph will show one plot. If it has e.g. three sub-list, it will show 3 plots. If there are no points at all in the plots the chart tends to throw errors, so each sub-list should have at least one point.

My GraphDataNotifier has a function addData(List<double> values) which basically updates all the plots with one value. So if I have 3 sublists, values should be of length 3. The first value should go into the first sublist, the second value into the second, etc. Here is the full code:

But…

Adding a point to the first sub-list added the point to all sub-lists. So for three plots, after adding one value to each plot, the length of each of the plots was 3. And after adding one more value, the length of each of the plots was 6, etc.

It took me some time to narrow it down, I thought that maybe I was calling addData() three times? Or perhaps I had a double for-loop somewhere?

Most of you probably already solved it: the problem was in the constructor:

GraphDataNotifier({this.numPlots = 2})
: super(List.filled(numPlots,[Data(0,0)])));

The List.filled() function puts the same object for all elements in the list. If the object is a native type e.g. int, this is great! But if it is a complex type e.g. a List, then it puts a reference to the same list for each element. In effect, putting one sub-list 3 times instead of 3 different sub-lists. In other words, this was a by reference vs by value bug, which I learned about in my first year of computer science…

The solution was simple: use List.generate() instead of List.filled():

And I got my 3 plots. Which goes to show:

Experience means that you solve the same bugs faster, not that you program less bugs

What is your “not this again” bug? How often does it happen to you?

--

--

Danielle H

I started programming in LabVIEW and Matlab, and quickly expanded to include Android, Swift, Flutter, Web(PHP, HTML, Javascript), Arduino and Processing.