access to inherited fields in dart - 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);
}
}

Related

Dart any differences between with or without abstract keyword?

Hi I just would like to know if there is any difference between giving abstract keyword or not like so.
// with
abstract class A {}
class B extends A {}
// without
class A {}
class B extends A {}
Should I give it?
With abstract you can omit implementations of methods and getters/setters
// with
abstract class A {
int foo();
String get bar;
set baz(String value);
}
var a = A(); // error about instantiating abstract class
class B extends A {
// error about missing implementations
}
var b = B(); // ok
// without
class A {
int foo(); // error about missing implementation
String get bar; // error about missing implementation
set baz(String value); // error about missing implementation
}
class B extends A {}

Missing concrete implementation of Getter and Setter

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

Dart type check using "Type"

How to check type of Super class with Child class instance? I have below example and don't want to use dart-mirrors.
class SomeClass{
}
class SomeOtherClass extends SomeClass{
}
void main() {
var s1 = new SomeOtherClass();
someMethod(SomeClass, s1);
}
void someMethod(Type t, dynamic instance){
print(instance.runtimeType == t);
//print(instance.runtimeType is t); Does not work!
}
Update
Just today the package reflectable was released which allows to do this like with mirrors, but a transformer generates code instead to avoid using mirrors in production.
import 'package:reflectable/reflectable.dart';
// Annotate with this class to enable reflection.
class Reflector extends Reflectable {
const Reflector()
: super(typeCapability); // Request the capability to invoke methods.
}
const reflector = const Reflector();
#reflector
class SomeClass{
}
#reflector
class SomeOtherClass extends SomeClass{
}
void someMethod(Type t, dynamic instance){
InstanceMirror instanceMirror = reflector.reflect(instance);
print(instanceMirror.type.isSubclassOf(reflector.reflectType(t)));
}
void main() {
var s1 = new SomeOtherClass();
someMethod(SomeClass, s1);
}
Original
It might be directly supported when https://github.com/gbracha/metaclasses is implemented.
Currently this workaround can be used:
class IsInstanceOf<E> {
bool check(t) => t is E;
}
void someMethod(Type t, dynamic instance){
print( new IsInstanceOf<t>().check(instance));
//print(instance.runtimeType is t); Does not work!
}
This runs fine and returns the correct result but the analyzer shows a warning because t can't be used as a type.
If you wrap SomeClass in a generic class it works without a warning
class SomeClass{
}
class SomeOtherClass extends SomeClass{
}
void main() {
var s1 = new SomeOtherClass();
someMethod(new IsInstanceOf<SomeClass>(), s1);
}
void someMethod(IsInstanceOf t, dynamic instance){
print(t.check(instance));
//print(instance.runtimeType is t); Does not work!
}
class IsInstanceOf<E> {
bool check(instance) => instance is E;
}
Try it at DartPad

#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 .

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