constructor in dart flutter code example
Example 1: dart this constructor
class MyClass {
int param1;
MyClass(this.param1);
}
final obj = MyClass(100);
class MyClassNamed {
int param1;
MyClassNamed({required this.param1});
}
final objNamedParam = MyClassNamed(param1: 100);
Example 2: constructor in dart
void main(){
SelfDrivingCar myLamb = SelfDrivingCar('Floride');
myLamb.drive();
}
class Car {
int numberofwheels = 4;
void drive() {
print('this is a car');
}
}
class SelfDrivingCar extends Car {
String destination='k';
SelfDrivingCar(String userDestination){
destination = userDestination;
}
@override
void drive() {
super.drive();
print('sterring wheel to $destination');
}
}
Example 3: fluter class constructor
class Customer {
String name;
int age;
String location;
Customer(String name, int age, String location) {
this.name = name;
this.age = age;
this.location = location;
}
}
Example 4: Flutter Constructor
Customer(String name, int age, String location) {
this.name = name;
this.age = age;
this.location = location;
}
Customer(this.name, this.age) {
this.name = name;
this.age = age;
}
Example 5: dart constructor
void main() {
Human jenny = Human(height1 :15);
print(jenny.height);
Human jerry = Human(height1: 20);
print(jerry.height);
}
class Human {
double height = 0;
Human({height1 = 0}) {
this.height = height1;
}
}
Example 6: constructoers in flutter
Customer(this.name, this.age, this.location);
Customer.withoutLocation(this.name, this.age);
Customer.empty() {
name = "";gd
location = "";
}
cfgdfgdfgdfggdfgdfdfgdfgdggdfgdfgdf