Named Constructor In Dart - Learn Dart Programming

preview_player
Показать описание
This video covers information about named constructor in dart. You will learn how to create named constructor with different examples.

Connect With Me:

#Dart #Constructor #DartConstructor #NamedConstructor #LearnDart
Рекомендации по теме
Комментарии
Автор

class Car {
String? name;
String? color;
double? prize;

Car({this.name, this.prize, this.color});

Car.second({this.name, this.color});

void display() {
print('Name: $name');
print('Color: $color');
print('Prize: $prize');

if (prize == null) {
prize = 0;
}
}
}

void main(){
Car honda= Car(name:'honda', color: 'black', prize:
honda.display();

Car bmw= Car(name: 'M4', color: 'Blue', prize:
bmw.display();
}

Lui_Catarino
Автор

in constructor you have map and then json, what it means and why itis there?

Thank you.

michalsaky
Автор

class Car {
String? name;
String? color;
var price;
//parameterized constructor
Car(this.name, this.color, this.price);

void dispaly() {
print("car name: $name");
print("car color: $color");
print("car price: $price");
}

//named constructor
Car.newcar(this.name, this.color, [this.price = "N/A"]);
}

void main(List<String> args) {
Car c = Car("BMW", "BLACK",
c.dispaly();
Car car = Car.newcar("AUDI", "RED");
car.dispaly();
}

BankITOfficer
Автор

Did the challenge

class Car {
//properties
String? name;
String? color;
String? price;

//Constructor 1
Car({required this.name, required this.color, this.price});
// Constructor 2
Car.other(this.name, this.color);

//Methods
display() {
if (price == null) {
price = "not stated";
}
print("This car is a $name");
print("its color is $color");
print("the price is $price");

}
}

void main(List<String> args) {
Car car1 = Car(name: "Benz", color: "white", price: "5000");
car1.display();

Car car2 = Car.other("BMW", "BLUE");
car2.display();
}

welshmanmakwara
Автор

class Car{

// properties

String? name;
String? color;
int? prize;

//Constructor
Car(this.name, this.color, this.prize);

//Name Constructor
Car.name(this.name, this.color);


// Methods & Function
void display(){

if(prize==null){

print('The car prize is $prize');
}

print('The car name is $name');
print('The car color is $color');


}

}



void main(){

// 1st object 3paremeter given

Car
car.display();



// 2nd object 2paremeter given
Car car2=Car.name('Porsche', 'Red');
car2.display();
}

Is the right???

yusufvinfotech
visit shbcf.ru