In dart, how to assign a value from a const Map to a const variable? - flutter

Let's say I have a ColorPalette class that looks like the following:
class ColorPalette {
static const Map<int, Color> gray = {
400: Color(0xFFDDDDDD),
500: Color(0xFFEEEEEE),
// ...
};
// Primary colors in separate variable
static const Color primaryBlue = Color(0xFF0000FF);
// ...
}
And if I were to assign a color value of the map to a variable that expects a const value:
class SomeOtherClass {
static const Map<String, Color> stateColor = {
// Error
'pressed': ColorPalette.gray[500],
}
}
Complains that "Const variables must be initialized with a constant value."
But this works fine:
...
'pressed': ColorPalette.primaryBlue,
...
Plus when assigning map, doing 500: const Color(...) or static const Map<int, Color> gray = const {...} didn't work either.
So I suspect that this is throwing error probably because compiler doesn't evaluate all entries in the map during compile time and therefore, the value being accessed with the given key can be only known during runtime?
Is there any workaround to assign value from a map to a variable that expects a const value?

There is no workaround.
An expression of the form e1[e2] cannot be a constant. The [] operator is a method (all user-definable operators are), and you cannot call a method at compile time except for a very small number of operations on known system types. Map lookup, even on constant maps, is not one of those exceptions.
The reason ColorPalette.primaryBlue works is that it directly references a const variable.

Related

Case expressions must be constant in Dart [duplicate]

Let's say I have a ColorPalette class that looks like the following:
class ColorPalette {
static const Map<int, Color> gray = {
400: Color(0xFFDDDDDD),
500: Color(0xFFEEEEEE),
// ...
};
// Primary colors in separate variable
static const Color primaryBlue = Color(0xFF0000FF);
// ...
}
And if I were to assign a color value of the map to a variable that expects a const value:
class SomeOtherClass {
static const Map<String, Color> stateColor = {
// Error
'pressed': ColorPalette.gray[500],
}
}
Complains that "Const variables must be initialized with a constant value."
But this works fine:
...
'pressed': ColorPalette.primaryBlue,
...
Plus when assigning map, doing 500: const Color(...) or static const Map<int, Color> gray = const {...} didn't work either.
So I suspect that this is throwing error probably because compiler doesn't evaluate all entries in the map during compile time and therefore, the value being accessed with the given key can be only known during runtime?
Is there any workaround to assign value from a map to a variable that expects a const value?
There is no workaround.
An expression of the form e1[e2] cannot be a constant. The [] operator is a method (all user-definable operators are), and you cannot call a method at compile time except for a very small number of operations on known system types. Map lookup, even on constant maps, is not one of those exceptions.
The reason ColorPalette.primaryBlue works is that it directly references a const variable.

Dart / Flutter nested classes static access

I'm trying to use a version of nested classes to make a tree of constant strings throughout my app in Flutter. I'd like them to be nested in order to find const strings quickly as the app grows, but still have the additional 'speed' of using the const keyword for Text() widgets.
However, I'm having a hard time trying to use them in const Text() widgets.
Here's a sample:
class Strings {
static const String ok = 'OK';
static TechnicianStrings technicianStrings = TechnicianStrings();
}
class TechnicianStrings {
TechnicianStrings();
final String createTech = 'Create Technician';
final String technician = 'Technician';
}
Throughout the app, I'd like to use these constant Strings as so:
const Text(Strings.ok), // <-- this works
const Text(Strings.technicianStrings.technician), //<-- only works without 'const'
const Text(Strings.technicianStrings.createTech), //<-- only works without 'const'
However, I get an error of "Arguments of a constant creation must be constant expressions" when I use the const keyword for the text widgets.
I've tried to use varying "const and static" names for the members of TechnicianStrings, and I get errors such as "invalid Constant" for the text widget. I also defined TechnicianStrings as static const, and got an error of 'Constant variables must be initialized with a constant value' for the line:
static const TechnicianStrings technicianStrings = TechnicianStrings();
Is there a way to use such a nested class structure hand in hand with const Text() widgets?
You need a constant constructor in TechnicianStrings.
class Strings {
static const String ok = 'OK';
static const TechnicianStrings technicianStrings = TechnicianStrings();
}
class TechnicianStrings {
const TechnicianStrings(); // <---
final String createTech = 'Create Technician';
final String technician = 'Technician';
}

Difference between static and const variable in Dart

Check these two examples:
static const inside of class:
class SessionStorage {
static const String _keySessionExist = 'storage.key';
}
Just a const outside of the class:
const String _keySessionExist = 'storage.key';
class SessionStorage {
}
Is there any difference or implications between having a static const variable inside of a class or just having it declared as const outside of it in Dart?
Maybe the compiled code changes?
Which one is more performant?
Which one should we follow if the variable is private to the file?
The declaration for cons must be using const. You have to declare it as static const rather than just const.
static, final, and const mean entirely distinct things in Dart:
static means a member is available on the class itself instead of on instances of the class. That's all it means, and it isn't used for anything else. static modifies members.
final means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies variables.
const has a meaning that's a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.
Const objects have a couple of interesting properties and restrictions:
They must be created from data that can be calculated at compile time. A const object does not have access to anything you would need to calculate at runtime. 1 + 2 is a valid const expression, but new DateTime.now() is not.
They are deeply, transitively immutable. If you have a final field containing a collection, that collection can still be mutable. If you have a const collection, everything in it must also be const, recursively.
They are canonicalized. This is sort of like string interning: for any given const value, a single const object will be created and re-used no matter how many times the const expression(s) are evaluated. In other words:
getConst() => const [1, 2];
main() {
var a = getConst();
var b = getConst();
print(a === b); // true
}
I think Dart does a pretty good job of keeping the semantics and the keywords nicely clear and distinct. (There was a time where const was used both for const and final. It was confusing.) The only downside is that when you want to indicate a member that is single-assignment and on the class itself, you have to use both keywords: static final.
Also:
I suggest you to have a look at this question
What is the difference between the "const" and "final" keywords in Dart?
Is there any difference or implications between having a static const variable inside of a class or just having it declared as const outside of it in Dart?
The obvious difference is that the static version must be referenced with the class name. Other than the change in name resolution, the should be the same.
Maybe the compiled code changes?
Which one is more performant?
They're both compile-time constants. There shouldn't be any difference.
Which one should we follow if the variable is private to the file?
If you want something that's private to a Dart library (which usually means the file), then prefix it with _. It doesn't matter whether it's global or static.

Dart: Is there a disadvantage to using const constructor?

There is an analyzer/lint check to warn me when it is possible to use a const constructor: https://dart-lang.github.io/linter/lints/prefer_const_constructors.html
(ie. using final a = const A(); instead of final a = A();)
I think to understand the advantages (there will only ever be one instance with the same constant values for a const constructor). But why isn't this the default? Since dart 2 the new can be omitted, so why didn't they change the definition of creating a new instance which can be created const simply as const instead of new? I assume there must be some disadvantage to having everything const?
(for example in a constant context like const [A()] it is actually the same as const [const A()], so why not everywhere)?
so why didn't they change the definition of creating a new instance which can be created const simply as const instead of new?
If you mean why doesn't final a = A(); automatically assume const A() if A has a const constructor:
Sometimes it is automatic:
const a = A();
in which case A's constructor is being invoked in a const context and doesn't need an extra const qualifier on the right-hand-side.
An explicit const expresses intent. For example, suppose you had:
final a = A(B());
where A and B have const constructors. Later, somebody makes a change:
final a = A(C());
where C does not have a const constructor. If const were automatic, then you would have no idea that a is no longer const. Maybe that's okay, but it also could suddenly have a negative impact on your application's performance, and without an explicit const qualifier, the impact of a local change could have a much wider scope than expected. (That said, explicit const qualifiers and automatically adding them aren't mutually exclusive.)
const can have downsides. const creates compile-time constants. If you have:
final a1 = A();
final a2 = A();
identical(a1, a2) is not true. If const A() were implicit, then identical(a1, a2) would be true, and maybe that's not a property that the code intended to have.
Compile-time constants live forever. The whole point is to have an object that can be reused instead of re-constructing it. The flipside is that won't be destroyed.

Getter using references and returning a constant variable

I have the following question: I define a class containing a private vector of (my) objects - i.e.:
vector<myOtherClass> myVector;
Then I would like to define a getter method which should not copy all objects saved in the vector. Thus I am always using references:
vector<myOtherClass> &getMyVector() const {
return (myVector);
}
The “const” means that I can only read the member variables in this method. But what should I do if I want that the returning variable is a constant - in particular what difference is between the three following possibilities (sometimes, the compiler allows me only to use one of them):
const vector<myOtherClass> &getMyVector() const {
return (myVector);
}
,
vector<myOtherClass> const &getMyVector() const {
return (myVector);
}
and
const vector<myOtherClass> const &getMyVector() const {
return (myVector);
}