Home > Back-end >  Dart flutter inharitance
Dart flutter inharitance

Time:11-10

I have a ParentClass with property _prop that you can get with a getter.

I have a ChildClass that needs this prop. The value of child's property must always be set to the value of parent prop. Child can't change it.

What is the right syntex for this?

CodePudding user response:

You can to do like this:

class ParentClass {
  ParentClass();
  
  get prop => _prop;
  
  dynamic _prop;
  
}

class ChildClass extends ParentClass{
  
  @override
  dynamic get prop => super._prop; // or super.prop in other class

  some() {
    print(prop); // it is ok
    prop = 5; // mistake
  }
}
  • Related