Best practice for overriding class fields in Dart

Tarique Naseem

I'm in the middle of creating a theming system for my app, where I wish to create a base theme class, with fields for colour and fonts:

abstract class BaseTheme {
  late Color backgroundColor;
  late Color appBarBackgroundColor;
  late Color progressIndicatorColor;

  late Color primaryColor;
  late Color iconColor;
}

(I have to define them as late to keep the Null Safety happy! These have to be overridden anyway, so no problems there).

I would then implement this base class for each theme, as below:

import 'base_theme.dart';

class LightTheme implements BaseTheme {
  Color backgroundColor = Colors.white;
  Color appBarBackgroundColor = Color(0xff1a1818);
  Color progressIndicatorColor = Color(0xffffc114);

  Color primaryColor = Colors.red;
  Color iconColor = Colors.white;
}

I would then use these values to populate the ThemeData:

BaseTheme _lightTheme = new LightTheme();

ThemeData _theme = ThemeData(
  scaffoldBackgroundColor: _lightTheme.backgroundColor,
  // etc...
}

Now, this appears to work fine. However, the page on overriding fields in the Linter for Dart states that overriding fields is bad practice. So, is there a better way to achieve something similar?

What's confusing is that, on that page, one example shows overriding fields when you extend a base class, which is considered bad. However, overriding fields when you implement a base class is good - But in this case, the field type has changed from Base to Derived (ie. they're not the same).

Any pointers (no pun intended) are most welcome!

jamesdlin

As the lint recommends, you should avoid overriding fields. Your abstract class should define an interface; as such, it should declare getters and setters and leave the implementation to the concrete, derived classes. Note that this avoids the need for late (which is an implementation detail that does not belong in the abstract base class).

abstract class BaseTheme {
  Color get backgroundColor;
  set backgroundColor(Color value);

  Color get appBarBackgroundColor;
  set appBarBackgroundColor(Color value);

  // etc.
}

Dart 2.13 allows members to be declared as abstract, so you can use that to avoid the repetitiveness:

abstract class BaseTheme {
  abstract Color backgroundColor;
  abstract Color appBarBackgroundColor;

  // etc.
}

Also see the GitHub issue: overridden_fields documentation is vague.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Best practice in Java for extending abstract class with overriding one method

From Java

Best practice: Extending or overriding an Android library project class

From Java

Best Practice: Initialize JUnit class fields in setUp() or at declaration?

From Java

Class best practice

From Dev

Static class best practice

From Dev

Best practice for recursive class

From Dev

Getting error while overriding class variables in dart

From

What is the Dart null checking idiom or best practice?

From Dev

What's the best practice to declare a variable in Dart?

From Dev

Django best practice for common class

From Dev

best practice to disable a class instanciating

From Java

Configuration class - best practice with Guice

From Dev

Compare Enum with Class, best practice?

From Dev

Angular.js best practice - extending controllers, overriding controller defaults

From Dev

Django save() overriding, best practice: on model, form or view?

From Dev

DART find all the fields in class

From Dev

What is a best practice for OCaml variants with many fields?

From Java

Best practice to choose fields for equals() implementation

From Dev

Kotlin best practice: val of object fields

From Dev

Is it best practice to create signature fields with relation values?

From Dev

Laravel Best Practice for Unique Fields Based on Variable

From Dev

MongoDb Best Practice | Insert "null" fields

From Dev

Class Instance or Inherited Class for a game? Best Practice

From Dev

PHP: Class Scope - external class best practice?

From Dev

Best practice to change current view from a class

From Dev

Executors reused in other class is the best practice?

From Java

Is using the Class instance as a Map key a best practice?

From Java

Which of these is the best practice for accessing a variable in a class?

From Dev

FMDatabase locked, best practice for usage within class

Related Related

  1. 1

    Best practice in Java for extending abstract class with overriding one method

  2. 2

    Best practice: Extending or overriding an Android library project class

  3. 3

    Best Practice: Initialize JUnit class fields in setUp() or at declaration?

  4. 4

    Class best practice

  5. 5

    Static class best practice

  6. 6

    Best practice for recursive class

  7. 7

    Getting error while overriding class variables in dart

  8. 8

    What is the Dart null checking idiom or best practice?

  9. 9

    What's the best practice to declare a variable in Dart?

  10. 10

    Django best practice for common class

  11. 11

    best practice to disable a class instanciating

  12. 12

    Configuration class - best practice with Guice

  13. 13

    Compare Enum with Class, best practice?

  14. 14

    Angular.js best practice - extending controllers, overriding controller defaults

  15. 15

    Django save() overriding, best practice: on model, form or view?

  16. 16

    DART find all the fields in class

  17. 17

    What is a best practice for OCaml variants with many fields?

  18. 18

    Best practice to choose fields for equals() implementation

  19. 19

    Kotlin best practice: val of object fields

  20. 20

    Is it best practice to create signature fields with relation values?

  21. 21

    Laravel Best Practice for Unique Fields Based on Variable

  22. 22

    MongoDb Best Practice | Insert "null" fields

  23. 23

    Class Instance or Inherited Class for a game? Best Practice

  24. 24

    PHP: Class Scope - external class best practice?

  25. 25

    Best practice to change current view from a class

  26. 26

    Executors reused in other class is the best practice?

  27. 27

    Is using the Class instance as a Map key a best practice?

  28. 28

    Which of these is the best practice for accessing a variable in a class?

  29. 29

    FMDatabase locked, best practice for usage within class

HotTag

Archive