Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart - Call Named Constructor using super vs class name

Tags:

flutter

dart

class Person {
  late String name;
  late int age;
  Person();
  Person.ap(this.name, this.age);
}

class Employee extends Person {
  Employee(String name, int age) : super.ap(name, age); // This works

  // This is giving error. The method 'ap' isn't defined in a superclass of 'Employee'.
  Employee(String name, int age) {
    super.ap(name, age);
  }
}

What's the difference of calling unnamed constructor using super or inside the parenthesis?

like image 394
Manivannan Avatar asked Dec 19 '25 21:12

Manivannan


1 Answers

try this, just create a setter function named ap in the Person class


     class Person {
          late String name;
          late int age;
          Person();
          Person.ap(this.name, this.age);
          
          ap(String name, int age){ //add this           
              this.name=name;
              this.age=age;
            }
        }
        
        class Employee extends Person {

          //here ap is name constructor 
          Employee(String name, int age) : super.ap(name, age);
          
          //here ap is setter function in the person class
          Employee.ap(String name, int age) {
            super.ap(name, age);
          }
        }
like image 199
narayann Avatar answered Dec 22 '25 09:12

narayann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!