Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The instance member 'params' can't be accessed in an initializer

Tags:

flutter

dart

class LevelUp extends GetxController {
  Map<String, String> params = Get.arguments;

  var myTest = params.[comLevel];
}

Error report--"The instance member 'params' can't be accessed in an initializer." I am new to programming and this is being called directly from a widget. I checked the LevelUp map and it has contents. The error occurs where I am trying to assign the param value to myTest. It doesn't matter if I put the key in quotes or provide an integer. Any advice would be greatly appreciated.

like image 474
LPark Avatar asked Aug 23 '20 02:08

LPark


3 Answers

You can't access params before you've initialized the object. To fix your example, move your myTest initialization into a constructor.

Also, I don't believe you should have a period before [comLevel].

class LevelUp extends GetxController {
  Map<String, String> params = Get.arguments;
  String myTest;
  
  LevelUp() {
    myTest = params[comLevel];
  }
}
like image 139
Lineous Avatar answered Oct 17 '22 14:10

Lineous


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;

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) {
    ...
  }
}

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();
  }
like image 24
gmdev Avatar answered Oct 17 '22 15:10

gmdev


Null safety update:

Use late keyword: Dart 2.12 comes with late keyword which helps you do the lazy initialization which means until the field bar is used it would remain uninitialized.

class Test {
  int foo = 0;
  late int bar = foo; // No error
}
like image 36
CopsOnRoad Avatar answered Oct 17 '22 16:10

CopsOnRoad