Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing concrete implementation of Getter and Setter

Tags:

dart

dart-pub

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.

like image 471
Shubhamhackz Avatar asked Sep 01 '25 00:09

Shubhamhackz


2 Answers

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.

like image 66
Günter Zöchbauer Avatar answered Sep 04 '25 08:09

Günter Zöchbauer


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

like image 20
anztrax Avatar answered Sep 04 '25 07:09

anztrax