Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I want set a value of extended class. How can i do it? Thanks.

Dart
 class A {
     String? value1A;    

   A({ 
    this.value1A, 
  });
}

class B extends A {

   String value1B = "";  

   value1A= ""; //<= I can't assing a value
 
}


What I have tried:

Dart
class B extends A {

   String value1B = "";
   

   value1A= "";
   super.value1A="";
   A newA; 
   newA.value1A= "";   
 
}
Posted
Updated 3-Mar-24 3:53am
v2

1 solution

The issue with your code is that you're trying to assign a value to value1A directly in the class body of B. In Dart, you can’t assign values to inherited variables directly in the class body. You should do this inside a method or constructor.

Dart
class A {
  String? value1A;

  A({ 
    this.value1A, 
  });
}

class B extends A {
  String value1B = "";  

  B() {
    value1A = ""; // Now you can assign a value
  }
}
 
Share this answer
 
v4
Comments
gacar 3-Mar-24 10:45am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900