0

What does = do here?

List<Segment> totalSegments = flight.departureFlight.segments;

Do both, totalSegments and flight.departureFlight.segments point to the same memory reference or totalSegments has the same data as flight.departureFlight.segments but points to a different memory location?
My understanding was that the latter should happen since dart is pass by value and not reference. However, a very annoying bug occurred when I added this line below that one:

totalSegments.addAll(flight.returnFlight.segments);

This above line actually modified the flight variable which in turn somehow modified the AsyncSnapshot from the StreamBuilder. Although, I wasn't using the variable anywhere else and not modifying other variables mentioned.
This all happened inside build function of a Stateless Widget. Maybe that has to do something with it.
I tried reading dart documentation for it, but either I couldn't find what I am looking for or the information is simply missing there. Read this too, but according to this, my use case shouldn't happen.

RoyalGriffin
  • 1,987
  • 1
  • 12
  • 25
  • have you declared ``List totalSegments = flight.departureFlight.segments;`` statement in the build() method? IF yes, it will cause change in totalSegments if you update ``flight.departureFlight.segments;`` since build method is recalled on ``setState`` – OMi Shah Dec 18 '19 at 17:25
  • My issue is actually the other way round. Changing `totalSegments` causes change in the actual `flight` object and at other places too. – RoyalGriffin Dec 18 '19 at 17:57
  • how's that even possible? Check if you've anywhere manipulated the actual ``flight`` – OMi Shah Dec 18 '19 at 17:59
  • I did not. I think when I am using = operator. It's just assigning the same reference to `totalSegment` as of `flight.departureFlight.segments` and hence the trouble. I don't know if it should behave like this or not is the question. – RoyalGriffin Dec 18 '19 at 18:18
  • no, it doesn't work like that. Making a change in ``flight.departureFlight.segments;`` can update ``totalSegments`` but not vice-versa. – OMi Shah Dec 18 '19 at 18:21
  • It **is** working like that. That's why the question. You can try it too, otherwise I'll create a sample project on github to re-create the issue. – RoyalGriffin Dec 18 '19 at 18:23
  • sure, please create a minimal code to represent your issue. I want to test. – OMi Shah Dec 18 '19 at 18:24
  • also, you didn't answer where you have declared ``List totalSegments = flight.departureFlight.segments;`` – OMi Shah Dec 18 '19 at 18:28

3 Answers3

0

When it comes to objects as in your case, you are assigning a reference to an existing object (memory location) to a new variable. While acting upon that reference, you change the same object.

If this is not what you intend, check out answers related to (deep) copying of the objects

Denis G
  • 511
  • 4
  • 11
0
List<Segment> totalSegments = flight.departureFlight.segments;

This expression does the following.

Assigns the value of the expression flight.departureFlight.segments to variable totalSegments.

This and only this and nothing more.
There is no need to know what is really happening, because this is what happens.

P.S.
What value will be obtained as a result of executing the expression flight.departureFlight.segments is another question, because it depends on the implementation of the members of the operands of the expression flight.departureFlight.segments.

mezoni
  • 10,684
  • 4
  • 32
  • 54
0

You were mistaken by the fact that Dart passes by value, and not by reference. Actually, it is exactly the opposite: (Almost) everything is always passed by reference (Which is usually a good thing!) Therefore, it is quite logical that because you edited totalSegments your departureflight.segments got edited too. It is a synonym. One of the ways to solve your problem would be:

List<Segment> totalSegments = List();
totalSegments.addAll(flight.departureFlight.segments.toList());
Cgrain
  • 149
  • 6