As stated in the title
Look at this code Example:
void main() {
final Student student = Student('Lincoln', 29);
print('Student before $student');
final Student newStudent = student;
newStudent?.name = 'Abraham';
print('new Student $newStudent'); /// 'Abraham', 29
print('Student after $student'); /// 'Abraham', 29 - but I need this output still 'Lincoln', 29
}
class Student {
Student(this.name, this.age);
String? name;
int? age;
@override
String toString() => '$name, $age';
}
From the code above if we set newStudent
and make changes, the student
variable also follows the changes, but I don’t want the student
variable changed. How to solve this?
2
Answers
You should make a new
Student
instance for the new one. If you want it to have the same name as age as the old you could do this for example:and also study this example..this will clear the concept…
so what u have to focus is
we passing reference not value by this statement