Dart / Flutter nested classes static access - flutter

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';
}

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.

Replace variable in dart

In consts class, I have declared this
Consts
static const URL = "";
In login bloc, after login successfully,server will return the url to me, and I assign the url to this variable.
Response user =
await _repo.getLogin(context, email, password);
var baseResponse = UserResponse.fromJson(user.body);
if (baseResponse.status == 101) {
Consts.URL = baseResponse.url;
}
In repo class, assume I have 5 methods, I need to use the url in consts class.Is it a good solution and possible?
Future create(){
try{
var request = http.MultipartRequest('POST',Uri.parse(Consts.URL));
}catch(e){
}
}
You cannot assign a value to a const during run time since const is a compile-time constant.
Please refer documentation on https://dart.dev/guides/language/language-tour#final-and-const
Final and const If you never intend to change a variable, use final or
const, either instead of var or in addition to a type. A final
variable can be set only once; a const variable is a compile-time
constant. (Const variables are implicitly final.) A final top-level or
class variable is initialized the first time it’s used.

Getting static constant to work for navigation

I have followed this guide: https://medium.com/flutter-community/flutter-vi-navigation-drawer-flutter-1-0-3a05e09b0db9
trying to replicate the drawer especially. However, I run into an error with the static constant in my Routes.dart file.
the codes looks like this:
in my lib/routes/routes.dart file:
import 'package:book_club/main.dart';
class Routes {
static const String Profile = ProfilePage.routeName;
static const String BookClub = BookClubPage.routeName;
static const String Library = LibraryPage.routeName;
static const String Search = SearchPage.routeName;
static const String Store = StorePage.routeName;
}
It gives me the following two errors:
error: Const variables must be initialized with a constant value. (const_initialized_with_non_constant_value at [book_club] lib/Routes/routes.dart:2)
error: Undefined name 'ProfilePage'. (undefined_identifier at [book_club] lib/Routes/routes.dart:2)
Anyone who can help me understand what I have done wrong? Tried going through the article several times but cannot find my misstake. I am a complete beginner in programming so hoping that there is an easy and obvious way to fix this :)
Using android studio if that makes a difference.
first import the ProfilePage package,
and
static const String Profile = ProfilePage.routeName; here the routeName of ProfilePage has to be static const as well, which means value of the variable should be known before compiling
class AClass{
static const yadu = '';
}
class BClass{
static const y = AClass.yadu;
}

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

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.

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);
}