class Shape {
String color;
void draw() {
print('Draw Random Shape');
}
}
class Rectangle implements Shape {
@override
void draw() {
print('Draw Rectangle');
}
}
Now the problem is I'm getting a warning saying
Missing concrete implementation of getter Shape.color and setter Shape.color
I know that every instance variable in dart has its own getter and setter implicitly.But in case of interface how do I fix this issue.I have also tried to looking at some of the similar questions on the stackoverflow but they'r not helpful.
Dart doesn't inherit implementations from implements Shape
, but only states that Rectangle
conforms the interface of Shape
.
You need to add String color;
to Rectangle
to satisfy implements Shape
.
You can do this by adding a field or alternatively a getter and a setter. Both are equivalent from a class' interface perspective.
class Rectangle implements Shape {
String color;
@override
void draw() {
print('Draw Rectangle');
}
}
or
class Rectangle implements Shape {
String _color;
String get color => _color;
set color(String value) => _color = value;
@override
void draw() {
print('Draw Rectangle');
}
}
The later is considered bad style if the getter and the setter only forward to a private field with no additional code.
or maybe you just want to extends
, no need to implements
?
you can changes your code into this ?
import 'package:meta/meta.dart';
abstract class Shape {
String color;
Shape({
@required this.color
});
void draw() {
print('Draw Random Shape');
}
}
class Rectangle extends Shape {
Rectangle() : super(
color: "#ff00ff"
);
@override
void draw() {
print('Draw Rectangle');
}
}
hope that can help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With