How to get default value of a field in an abstract class How to get default value of a field in an abstract class dart dart

How to get default value of a field in an abstract class


General answers:

  • You can use super.field if you're extending a class and want to access its fields/getters.
  • If you're reflecting on a class declaration, you can't currently introspect on any bodies, including method bodies, or initializers. You would have to use the analyzer or dart2js.
  • Dado is... interesting in how it works. It tries to instantiate the abstract class, and doesn't actually work anymore.

Since you were looking at Dado, let me explain more how it works, even though it's not a general answer to this question.

First, the approach with Dado was and is pretty experimental. I was trying to use the language features to require a declarative definition of modules so that authors couldn't write modules that didn't work with code-generation. There have been some changes in Dart and mirrors that make this approach less workable, or at least makes it require more boilerplate, so I'm rethinking the whole thing. Take any technique you find there with a gain or two of salt :)

For anyone else who hasn't looked at a sample or the code, in Dado you declare an abstract class for a module definition. A field with a value is declaring a fixed singleton value, similar to how a more traditional DI container might have you write bind(Foo).toValue(new Foo()).

The way I (used to) get the value is by instantiating the abstract class via mirrors, and simply then simply read the field. dart:mirrors changed to disallow instantiating abstract classes, so this no longer works. This is unfortunate, because other types of bindings were declared with abstract methods, and those cause warnings in non-abstract classes.