The following code
class MemberException extends ServerException {
String message;
MemberException(message) {
super(message);
}
}
class ServerException implements Exception {
String message;
ServerException(this.message);
}
produces the following (somewhat unhelpful) error message
Too few arguments in implicit super() constructor invocation in '(String) -> dynamic'
The correct format is:
class MemberException extends ServerException {
String message;
MemberException(message) : super(message) {
// constructor body
}
}
You need to initialize super before the constructor body is called.
Ref: http://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#ch02-constructors (see the part on initializers)
Related
The following example of a factory constructor (using a static method) compiles fine:
class Base<T> {
Base();
static Base myFactory(bool isInt) {
if (isInt) { return A(); }
else { return B(); }
}
}
class A extends Base<int> {}
class B extends Base<String> {}
However, if I use the syntax for a factory constructor:
class Base<T> {
Base();
factory Base.myFactory(bool isInt) {
if (isInt) { return A(); }
else { return B(); }
}
}
class A extends Base<int> {}
class B extends Base<String> {}
I get:
A value of type 'A' can't be returned from the method 'myStatic' because it has a return type of 'Base<T>'.
Why is this an error? A is a subtype of Base<int>, so isn't it a subtype of the generic Base<T>?
If you don't specify a type, it is dynamic by default. Thus, in the first case, the return type of the myFactory method is Base<dynamic>, while in the other case, the return type of Base.myFactory method is Base<T>.
I'm trying to specify a function parameter as a generic type T:
enum MyEnum {
Foo,
Bar
}
class DbColumn {
final Function<T>(T value) serializer;
const DbColumn({this.serializer});
}
class MyClass {
static final DbColumn rating = DbColumn(
serializer: <MyEnum>(v) {
var i = v.index;
}
);
}
However when trying to access index on v I get this type error message:
The getter 'index' isn't defined for the type 'Object'.
Try importing the library that defines 'index', correcting the name to the name of an existing getter, or defining a getter or field named 'index'.
When I hover over v in VSC it says that it's of type MyEnum.
If I instead remove the generic type and do a cast like this it works as expected:
class DbColumn {
final Function(dynamic value) serializer;
const DbColumn({this.serializer});
}
class MyClass {
static final DbColumn rating = DbColumn(
serializer: (v) {
var casted = v as MyEnum;
var i = casted.index;
}
);
}
Why is the generic type not working as expected?
EDIT:
What is even weirder is that this example works too if I put it inside MyClass:
x<T>(Function(T) c) {}
y() {
x<MyEnum>((v) {
print(v.index); // No error given and type of v is MyEnum
});
}
EDIT 2: The same problem happens when overriding methods:
abstract class MyInterface {
int someFunction<T>(T value);
}
class MyClass implements MyInterface {
#override
someFunction<MyEnum>(v) {
return v.index; // Gives same error and no intellisense happens in VSC
}
}
Instead of making the function generic, declare the class as generic and it will work as expected. Like this :
enum MyEnum {
Foo,
Bar
}
class DbColumn<T> {
final Function(T value) serializer;
const DbColumn({this.serializer});
}
class MyClass {
static final DbColumn<MyEnum> rating = DbColumn(
serializer: (v) {
var i = v.index;
print('Index : $i');
}
);
}
void main() {
MyClass.rating.serializer(MyEnum.Bar);
}
OUTPUT :
Index : 1
In python you can run a simple function inside of a __init__ method like this:
class AuthError:
def __init__(self, message):
self.message = message
print(self.message)
how can I do this in dart? (I'm fairly new to dart and flutter)
class AuthError {
final String message;
MyExample(this.message); // <-- how do I e.g. print message when AuthError is initialized?
}
Add a constructor to you class. The constructor should be named the same as the class it is part of:
class AuthError {
final String message;
AuthError(this.message) {
print('AuthError is initialized');
}
}
void main() {
AuthError('string'); // AuthError is initialized
}
Read more about here: https://dart.dev/guides/language/language-tour#constructors
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 {}
I'm trying to register a custom generic value converter but it's not being picked up in the binding process. What can be wrong. It's based on this manual: https://docs.grails.org/latest/guide/theWebLayer.html#dataBinding
When I remove the generics everything works fine.
my generic enum converter:
abstract class EnumValueConverter < T extends Enum > implements ValueConverter {
#Override
boolean canConvert(Object value) {
value instanceof String
}
#Override
Object convert(Object value) {
try {
T.valueOf(value)
} catch (IllegalArgumentException illegalArgumentException) {
throw new IllegalArgumentException("needs to be one of ${T.values()*.name()} but is: $value")
}
}
#Override
Class<?> getTargetType() {
T
}
}
my specific converter:
class SomeEnumValueConverter extends EnumValueConverter<SomeEnum>{}
registartion in resources.groovy:
someEnumValueConverter SomeEnumValueConverter
You can't use a generic in a context like that. If you create an instance of SomeEnumValueConverter and invoke getTargetType() on that instance the return value will be Object, not SomeEnum so the framework doesn't know what type the converter should be used for.
You can override the getTargetType() method in SomeEnumValueConverter and return SomeEnum.