Missing concrete implementation of Getter and Setter - dart

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

Related

Binding between an Object and a SimpleIntegerProperty

I have a combo box over my GUI in JavaFX.
This Combo Box is composed of a complex type elements :
public class DureeChoiceBoxElement extends ObservableValueBase<DureeChoiceBoxElement> {
private IntegerProperty duree;
#Override
public String toString() {
return duree.get() + " an";
}
}
I want to map (or bind) the selected complex element with my model which contains the simple type :
public class Pel {
private IntegerProperty duree = new SimpleIntegerProperty(1);
public Property<Number> dureeProperty() {
return duree;
}
public void setDuree(Integer duree) {
this.duree.setValue(duree);
}
public Integer getDuree() {
return duree.getValue();
}
}
How to do it ?
I tried in the controller with :
public class PelController {
#FXML
private ChoiceBox<DureeChoiceBoxElement> duree;
//etc..
pel.dureeProperty().bind(createElapsedBindingByBindingsAPI2(duree.getValue()));
/*
* #return an ObjectBinding of immutable TimeElapsed objects for the player
*/
private ObjectBinding<Property<Number>> createElapsedBindingByBindingsAPI2(
final DureeChoiceBoxElement dureeChoiceBoxElement) {
return Bindings.createObjectBinding(new Callable<Property<Number>>() {
#Override
public IntegerProperty call() throws Exception {
return dureeChoiceBoxElement.dureeProperty();
}
}, dureeChoiceBoxElement.dureeProperty());
}
}
But it doesn't work (even not compile). I want to say that "Bind this simple property to this complex Object calling the method I give you through the method named "createElapsedBindingByBindingsAPI2(..)".
It is logical read but I didn't managed to make it works anyway.
That's poor ....
Any help please :).
Example that (obviously) works with legacy code style (Swing coding) :
duree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<DureeChoiceBoxElement>() {
#Override
public void changed(ObservableValue<? extends DureeChoiceBoxElement> observable,
DureeChoiceBoxElement oldValue, DureeChoiceBoxElement newValue) {
// changement durée
log.debug("Durée sélectionnée : {}", duree.getSelectionModel().getSelectedItem().getDuree());
log.debug("Durée bindée ? : {}", pel.getDuree());
pel.setDuree(duree.getSelectionModel().getSelectedItem().getDuree());
}
});
Like this my model is set to selected item. But it implies some boilerplate code. Any better idea based on high level bindings of JavaFX ?

JavaFX binding a BooleanProperty to Paint

I am writing a code that can take some boolean values from a part of some other code and change colours of certain circles on the screen accordingly. However I ran into problems trying to bind the boolean values to colours. I ended up with this:
unit1.getNeuron().getWorkingProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue == Boolean.FALSE) {
controller.paint1 = new ObservableValueBase<Paint>() {
#Override
public Paint getValue() {
return Color.RED;
}
};
} else {
controller.paint1 = new ObservableValueBase<Paint>() {
#Override
public Paint getValue() {
return Color.DODGERBLUE;
}
};
}
}
});
but I'll have to repeat it for n times for n variables I use. Is there a different way to implement this?
Let' s say you want to create an ObservableObjectValue<Paint> you want to toggle based on an ObservableBooleanValue, then Bindings is your friend:
final ObservableBooleanValue booleanCondition = unit1.getNeuron().getWorkingProperty();
final ObservableObjectValue<Paint> paintProperty = Bindings.when(booleanCondition).then(Color.RED).otherwise(Color.DODGERBLUE);

#override of Dart code

I noticed PetitParserDart has a lot of #override in the code, but I don't know how do they be checked?
I tried IDEA dart-plugin for #override, but it has no effect at all. How can we use #override with Dart?
From #override doc :
An annotation used to mark an instance member (method, field, getter or setter) as overriding an inherited class member. Tools can use this annotation to provide a warning if there is no overridden member.
So, it depends on the tool you use.
In the current Dart Editor(r24275), there's no warning for the following code but it should (it looks like a bug).
import 'package:meta/meta.dart';
class A {
m1() {}
}
class B extends A {
#override m1() {} // no warning because A has a m1()
#override m2() {} // tools should display a warning because A has no m2()
}
The #override annotation is an example of metadata. You can use Mirrors to check for these in code. Here is a simple example that checks if the m1() method in the child class has the #override annotation:
import 'package:meta/meta.dart';
import 'dart:mirrors';
class A {
m1() {}
}
class B extends A {
#override m1() {}
}
void main() {
ClassMirror classMirror = reflectClass(B);
MethodMirror methodMirror = classMirror.methods[const Symbol('m1')];
InstanceMirror instanceMirror = methodMirror.metadata.first;
print(instanceMirror.reflectee); // Instance of '_Override#0x2fa0dc31'
}
it's 2021 . the override it's optional
Use the #override annotation judiciously and only for methods where the superclass is not under the programmer's control, the superclass is in a different library or package, and it is not considered stable. In any case, the use of #override is optional. from dart api https://api.dart.dev/stable/2.10.5/dart-core/override-constant.html
example
Class A {
void say (){
print ('Say something 1') ;
}
}
Class B extends A {
#override
void adds() { // when i don't type the same name of super class function show an
// warning not an error say 'The method doesn't override an inherited
// method.' because it's not same name but when type the same name must be
// overriding
print ('Say something 2 ')
}
Update : the main use of #override is when try to reach abstract method inside abstract class in sub class that inherited to the abstract super class . must use #override to access the abstract method .

access to inherited fields in dart

abstract class Painter {
CanvasElement canvas;
Painter(this.canvas);
void draw();
}
class SpritePainter extends Painter{
SpritePainter(this.canvas);
void draw(){
window.console.log("Drawing");
window.console.log(canvas);
}
}
using the above code my application fails when trying to call new SpritePainter(query('#sprite-canvas')); saying that this.canvas is an unknown field. I thought the CanvasElement in the abstract parent class is accessible to the sub class?
Update:
I fixed this with:
SpritePainter(CanvasElement canvas):super(canvas);
but then I read on dart tutorials that abstract classes can only have factory constructors?
You have to forward your param to the super constructor like the following :
abstract class Painter {
CanvasElement canvas;
Painter(this.canvas);
void draw();
}
class SpritePainter extends Painter{
SpritePainter(CanvasElement canvas) : super(canvas);
void draw(){
window.console.log("Drawing");
window.console.log(canvas);
}
}

AndroidAnnotations in CursorAdapter

I am developing for android using android annotations but I don't unterstand how to use it with CursorAdapters.
There is already a example for BaseAdapters, but if I add #EBean to a class that extents CursorAdapter I get the error message "#EBean annotated element should have a constructor with one parameter max, of type android.content.Context". CursorAdapter already has two constructors.
public class SongInfoAdapter extends CursorAdapter {
...
#Override
public void bindView(View view, Context context, Cursor cursor) {
...
rowData.id.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
itemOnClick(rowData);
}
});
}
public void itemOnClick(RowDataHolder rowData) {
switch(audioPlayer.getPlayingplayer()) {
case AudioPlayer.RIGHT:
case AudioPlayer.NONE:
audioPlayer.load(rowData.songInfo, AudioPlayer.LEFT);
break;
case AudioPlayer.LEFT:
audioPlayer.load(rowData.songInfo, AudioPlayer.RIGHT);
break;
}
}
...
}
AudioPlayer is a class that uses annotations (#EBean), but I can't write
#Bean
AudioPlayer audioPlayer;
because I can't use annotations in this class. How can I use AndroidAnnotations in CursorAdapter?
Many thanks in advance .
Create a constructor that takes one argument, the context.
SongInfoAdapter (Context context) {
super(context, null, FLAG_AUTO_REQUERY);
}
Create an init method and set the cursor for the adapter in init.
public void init(Cursor c) {
this.changeCursor(c);
}
Now you can annotate SongInfoAdapter with #EBean and use annotations.

Resources