top of page

[dart]Variable values change unintentionally


Situation


There is a class that regulates the operation of the application. The "Change settings" screen takes a copy of the existing instance as an argument and displays the setting values on the screen. Press the "Save" button to replace the copy of this instance with an existing instance. If you return without pressing the "Save" button, the existing instance will be retained.


However, even if you operate it to some extent and return without pressing the "Save" button, the existing instance has been changed.



Cause



When creating the "Change settings" screen, the instance was assigned with =.


In dart (or in most languages like C#), even if you assign a class with =, it will be passed by reference. In other words, the address of the variable is passed, so if you change the value of the assignment destination, the original value will also change.


In the above example, I intend to change the value of the instance copy on the "Change settings" screen, but I have also changed the original instance value, and I do not have to press the "Save" button to replace the instance.



Solution


You need to create a new instance without passing it with =. It seems that there is no method that copies the entire class in dart, so it is convenient to prepare a method that can inherit the original value.

class settingData{
  String name;
  int id;

  //Constructor that copies member variables to create a new instance
  settingData.copyFrom(settingData source){
    this.name=source.name;
    this.id=source.id;
  }
}
class SettingPage extends StatefulWidget{
  final settingData data;
    
  SettingPage({this.data})
}

//~~~~Some class~~~~~~

Future<void> goToSettingPage(settingData data) async{
  await Navigator.of(context).push(new MaterialPageRoute(
    builder: (context) =>
    // SettingPage(data: data)   NG example
    SettingPage(data: settingData.copyFrom(data))  //here
  ));
}

However, this method is not very smart because you have to change the copyFrom() constructor every time you add a new member variable to the settingData class.



Lastly


It's a fairly rudimentary mistake, but I think there are many situations like the one above (using tentative data until the user approves). Be careful when passing variables.

Recent Posts

See All

[Flutter/Dart] Format string with TextField

What want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to display it in 3-digit delimiters with a ¥ prefix. Rather than ha

Comments


Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page