class LevelUp extends GetxController {
Map<String, String> params = Get.arguments;
var myTest = params.[comLevel];
}
Run Code Online (Sandbox Code Playgroud)
错误报告--“无法在初始化程序中访问实例成员 'params'。” 我是编程新手,这是直接从小部件调用的。我检查了 LevelUp 地图,它有内容。错误发生在我试图将 param 值分配给 myTest 的地方。我是否将键放在引号中或提供一个整数都没有关系。任何建议将不胜感激。
Cop*_*oad 64
使用late关键字:Dart 2.12 附带了late关键字,它可以帮助您进行延迟初始化,这意味着在bar使用该字段之前它将保持未初始化状态。
class Test {
int foo = 0;
late int bar = foo; // No error
}
Run Code Online (Sandbox Code Playgroud)
Lin*_*ous 36
params在初始化对象之前您无法访问。要修复您的示例,请将您的myTest初始化移动到构造函数中。
另外,我不认为你应该有一段时间之前[comLevel]。
class LevelUp extends GetxController {
Map<String, String> params = Get.arguments;
String myTest;
LevelUp() {
myTest = params[comLevel];
}
}
Run Code Online (Sandbox Code Playgroud)
gmd*_*dev 12
Although this question has been answered for the OP's case, I want to offer a solution to those receiving this error in a StatefulWidget scenario.
Consider a situation where you would want to have a list of selectable items that dictate which category to display. In this case, the constructor might look something like this:
CategoryScrollView({
this.categories,
this.defaultSelection = 0,
});
final List<String> categories;
final int defaultSelection;
Run Code Online (Sandbox Code Playgroud)
Note the property defaultSelection is responsible for specifying which category should be selected by default. You would probably also want to keep track of which category is selected after initialization, so I will create selectedCategory. I want to assign selectedCategory to defaultSelection so that the default selection takes effect. In _CategoryScrollViewState, you cannot do the following:
class _CategoryScrollViewState extends State<CategoryScrollView> {
int selectedCategory = widget.defaultSelection; // ERROR
@override
Widget build(BuildContext context) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
The line, int selectedCategory = widget.defaultSelection; will not work because defaultSelection it hasn't been initialized yet (mentioned in other answer). Therefore, the error is raised:
The instance member 'widget' can't be accessed in an initializer.
The solution is to assign selectedCategory to defaultSelection inside of the initState method, initializing it outside of the method:
class _CategoryScrollView extends State<CategoryScrollView> {
int selectedCategory;
void initState() {
selectedCategory = widget.defaultSelection;
super.initState();
}
Run Code Online (Sandbox Code Playgroud)
一个简单的例子,它展示了我们如何解决上述问题,示例:创建类 B 的实例,并在其参数中传递类 A 的实例
错误(初始化程序的编译时错误):
final A _a = A();
final B _b = B(_a);
Run Code Online (Sandbox Code Playgroud)
显示错误:无法在初始化程序中访问实例成员“_a”。
正确的:
final A _a = A();
late final B _b;
AppointmentRepository() {
_b = B(_a);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您不能使用实例成员作为类中变量的初始化。如果可以将成员设为static,那就是解决问题的最简单、最安全的方法。Dart 文档中也有介绍:链接
解决方案:
class LevelUp extends GetxController {
static Map<String, String> params = Get.arguments;
var myTest = params.[comLevel];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35215 次 |
| 最近记录: |