Wil*_*ill 3 constants dart flutter
我正在尝试使用嵌套类的一个版本在 Flutter 中的整个应用程序中创建常量字符串树。我希望它们能够嵌套,以便随着应用程序的增长快速找到 const 字符串,但仍然具有使用 Text() 小部件的 const 关键字的额外“速度”。
但是,我很难尝试在 const Text() 小部件中使用它们。
这是一个示例:
class Strings {
static const String ok = 'OK';
static TechnicianStrings technicianStrings = TechnicianStrings();
}
class TechnicianStrings {
TechnicianStrings();
final String createTech = 'Create Technician';
final String technician = 'Technician';
}
Run Code Online (Sandbox Code Playgroud)
在整个应用程序中,我想像这样使用这些常量字符串:
const Text(Strings.ok), // <-- this works
const Text(Strings.technicianStrings.technician), //<-- only works without 'const'
const Text(Strings.technicianStrings.createTech), //<-- only works without 'const'
Run Code Online (Sandbox Code Playgroud)
但是,当我对文本小部件使用 const 关键字时,出现“常量创建的参数必须是常量表达式”的错误。
我尝试对 TechnicianStrings 的成员使用不同的“const 和 static”名称,但出现诸如文本小部件“无效常量”之类的错误。我还将 TechnicianStrings 定义为 static const,并收到以下行的错误:“常量变量必须使用常量值进行初始化”:
static const TechnicianStrings technicianStrings = TechnicianStrings();
Run Code Online (Sandbox Code Playgroud)
有没有办法将这样的嵌套类结构与 const Text() 小部件一起使用?
您需要一个常量构造函数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';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2387 次 |
| 最近记录: |