Maritn Ge
Maritn Ge

Reputation: 1226

How can I have a class with a dynamic member that is a fixed type in its children in Dart?

I have a class - Setting. This class looks as follows:

abstract class BaseSetting {
  final dynamic value;
  final String key;
  final bool secure;
  final String description;
//...

I also have another class for other kinds of Settings - like numbers, ranges, text, etc. I want the BaseSetting to have a value ofc, (I think), so I tried BaseSetting<T>and the value not being dynamic - but T.

I am not sure if my approach will work but the idea is that EVERY setting has a value, but every child-setting has a fixed type for that value.

(This is my approach to Settings (refresh / minute, language, metric / imperial etc.) by the way - if what I want to achieve is possible but it is a bad way to do so, please comment so I can look at a better approach.)

Upvotes: 1

Views: 157

Answers (1)

Jean Lucas
Jean Lucas

Reputation: 282

try to do like this:

abstract class BaseSetting<T> {
  final T value;
  final String key;
  final bool secure;
  final String description;

  BaseSetting(
    this.value,
    this.key,
    this.secure,
    this.description
  );
}

Upvotes: 1

Related Questions