0

Below is an attempted nested object.

class AppColors {
  static const light = {
    final backgroundSolid = const Color.fromRGBO(255, 255, 255, 1);
    final backgroundPrimary = const Color.fromRGBO(255, 255, 255, 0.84);
  },
  static const dark = {
      final backgroundSolid = const Color.fromRGBO(0, 0, 0, 1);
      final backgroundPrimary = const Color.fromRGBO(0, 0, 2550 0.84);
  }
}

The error is on the use of final on the first line item. What is the correct way of creating nested objects?

2
  • 1
    Hmm, do you come from JS, in which you nest object using {}?, that is equivalent to the Set data structure in Dart. Also what API/design do you want to achieve by nesting these values? Don't mind the downvote and please do ask away any doubt that you have. That's what SO is for. Commented Oct 22, 2021 at 15:33
  • Yes I would have found this very straightforward in JS. The purpose is that I can use this class as an effective data set of colors whereby I can call colors relative to theme data. Updated the question to show more detail. Commented Oct 23, 2021 at 10:24

1 Answer 1

1

Dart is a pretty flexible language, there is not really a correct way

You could certainly implement it using Maps, but you would lose the autocomplete and safety that a class has:

class AppColors {
//These are maps, note the `:` to separate key and values
  static const light = {
  'backgroundSolid' : const Color.fromRGBO(255, 255, 255, 1);
   'backgroundPrimary' : const Color.fromRGBO(255, 255, 255, 0.84);
};
static const dark = {
 'backgroundSolid' : const Color.fromRGBO(0, 0, 0, 1);
'backgroundPrimary' : const Color.fromRGBO(0, 0, 2550 0.84);
};
}

void main()
{
final  backgroundLight=AppColors.light['backgroundPrimary'];

}

However this is how I would code it. This way you have a class holding the attributes you need, if more are added, the dart analyzer will make you refactor the factory constructors to add/delete new fields. And the access to the properties feels quite clean too.

class AppColors {
  final Color backgroundSolid;
  final Color backgroundPrimary;

  AppColors({required this.backgroundSolid, required this.backgroundPrimary});

  factory AppColors.light()=>
      AppColors(backgroundSolid: const Color.fromRGBO(255, 255, 255, 1),
          backgroundPrimary: const Color.fromRGBO(255, 255, 255, 0.84));
  factory AppColors.dark()=>
      AppColors(backgroundSolid: const Color.fromRGBO(0, 0, 0, 1),
          backgroundPrimary: const Color.fromRGBO(0, 0, 0, 0.84));

}

void main()
{
final  backgroundLight=AppColors.light().backgroundPrimary;

}

Sign up to request clarification or add additional context in comments.

2 Comments

I implemented almost exactly this earlier today. The only difference is that I placed that I turned it around so that backgroundSolid came first and light or dark were placed in a map. This allowed me to avoid ternary operators in the main app and pass the themeMode (ie. light or dark) to App Colors. So in app I have AppColors.backgroundSolid[themeMode] where theme mode is light or dark. Thanks very much for your help, R
Glad you figured it out, and sorry for the late response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.