Variable in class not updating when changing value

Jessica

I have a variable named number and a class which sets a variable to number in its constructor. I then change the value of number but the number in the class doesn't update. Here's my code:

void fn1() {
  int number = 5;
  SomeClass someClass = SomeClass(number);
  // Do some other things which take a long time
  number = 2;
  someClass.printer(); // prints 5. expect 2
}

class SomeClass {
  int nestedNum;

  SomeClass(this.nestedNum) {
    printer();
  }

  void printer() {
    print(nestedNum);
  }
}

How can I get nestedNum to update anytime number is changed?

So the hacky/easy solution would be anytime I change number in fn1, I need change nestedNum in someClass: like this: someClass.nestedNum = 2. The issue with that is if I have multiple classes I would have to go through each one to change the variable.

Another solution I thought of, was to never pass number into someClass, but instead pass this and access number using this. The issue with that is again, if I have multiple classes each needing to access a different variable from fn1.

lrn

The only way for nestedNum to change when number changes is to let nestedNum read from number. Dart does not allow references or pointers to variables to be passed around. What you can do is to pass a function to SomeClass which provides the value each time it's needed.

void fn1() {
  int number = 5;
  SomeClass someClass = SomeClass(() => number);
  // Do some other things which take a long time
  number = 2;
  someClass.printer(); // prints 2!
}

class SomeClass {
  final int Function() _nestedNum;
  int get nestedNum => _nestedNum();

  SomeClass(int Function() nestedNum) : _nestedNum = nestedNum {
    printer();
  }

  void printer() {
    print(nestedNum);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Tkinter Checkbutton not updating when changing variable

chart not updating when changing the value of displaymember

Array Changing to String When Updating Value

Why is the class variable's value being changed when I'm changing the value in an object (Python 3.11)?

Python - Changing Class Variable Value in Function

changing the value of Mainactivity variable in another class

changing variable value outside a function inside a class

Changing @State variable to value from UserDefaults on load not updating Picker

variable value is not changing when i am changing the value with a function in js

Instance variable changing value when another instance of same class is modified, Python-Tkinter

Variable not updating its value when Returning it

What is the difference between += and = when changing the value of a variable?

`alert` Error When Changing Variable Value

changing the value of a variable when a radio button is clicked

knockout - UI not updating when changing single value of a column in observable array

Angular 6 ngFor values is updating when changing the input value

Class not changing when dropdown value is selected with JQuery

AngularJS 2: Changing the value of a variable outside of the class that the variable belongs to

Changing a variable in one object is updating same variable of same class's another object

Class instance variable not updating

Python updating class variable

(Kivy) Updating a class variable

Changing the name of a variable in pojo class into a user input value in kotlin

Why is my class variable changing its value between methods?

Global variable changing value over another class (C#)

Recording Class Var Status while changing variable value in C#

Changing values of a Class variable

text object not updating on screen when variable value changes

Updating a variable value when Leaflet popup is clicked in svelte