Home > Enterprise >  Flutter&Dart: How to remove references between parameter
Flutter&Dart: How to remove references between parameter

Time:08-11

I have a global parameter template for any parameter that needs to copy these data to show in any widget. Some widgets have a function that changes the data in parameters. The issue is when the function changes the data of a parameter by using .add() or .addAll(), the global parameter data also changes. The data type that will change is List and Map.

Example:

dynamic a;
dynamic b;
a = [];
b = a;
print("List");
print("a = $a , b = $b"); //a = [] , b = []
b.addAll([1]);
print("a = $a , b = $b"); //a = [1] , b = [1]
b = [
  ...b,
  ...[2]
];
print("a = $a , b = $b"); //a = [1] , b = [1, 2]

a = {};
b = a;
print("Map");
print("a = $a , b = $b"); //a = {} , b = {}
b.addAll({'1': 1});
print("a = $a , b = $b"); //a = {1: 1} , b = {1: 1}
b = {
  ...b,
  ...{'2': 2}
};
print("a = $a , b = $b"); //a = {1: 1} , b = {1: 1, 2: 2}

How to solve this with using .add() or .addAll()?

CodePudding user response:

Instead of assigning the same value you should assign a copy. To do that you can call toList() on it. In this example you also need to cast it to List first because you declared them as dynamic. So like

b = (a as List).toList();

For the map you can do this instead:

b = {...(a as Map)};

Edit: it seems the casting to List isn't necessary, but the casting to Map is

CodePudding user response:

Firstly, try to avoid using dynamic whenever possible (of course, sometimes it is not avoidable). In your example List<int> seems to be good.

Secondly, lists (and maps) are passed by reference. For example:

final a = <int>[];
final b = a;

If you add an element to b, a changes as well because they refer to the same list. To prevent this, do final b = List.of(a), which creates a new list that has the same elements as a, then you can do whatever you want to b without touching a.

CodePudding user response:

You can create a new instance while assigning items like

  dynamic a;
  dynamic b;
  a = [];
  b = a.toList();
  print("List");
  print("a = $a , b = $b"); //a = [] , b = [1]

CodePudding user response:

Update: Thank Yeasin Sheikh for easy step with list -> b = a.toList();. Thank Ivo for easy step with map -> b = {...(a as Map)};.

Aw, I notice in my example and see that. Just use ..., or .addAll()

dynamic a;
    dynamic b;
    a = [0];
    b = [...a];
    print("List 1");
    print("a = $a , b = $b");
    b.addAll([1]);
    print("a = $a , b = $b");

    a = [0];
    b = []   a;
    print("List 2");
    print("a = $a , b = $b");
    b.addAll([1]);
    print("a = $a , b = $b");

    a = [0];
    b = [];
    b.addAll(a);
    print("List 3");
    print("a = $a , b = $b");
    b.addAll([1]);
    print("a = $a , b = $b");

    a = {'0': 0};
    b = {
      ...{},
      ...a,
    };
    print("Map");
    print("a = $a , b = $b");
    b.addAll({'1': 1});
    print("a = $a , b = $b");

result:

List 1
a = [0] , b = [0]
a = [0] , b = [0, 1]
List 2
a = [0] , b = [0]
a = [0] , b = [0, 1]
List 3
a = [0] , b = [0]
a = [0] , b = [0, 1]
Map
a = {0: 0} , b = {0: 0}
a = {0: 0} , b = {0: 0, 1: 1}
  • Related