88
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.

Francesco - FL
  • 603
  • 1
  • 4
  • 25
LPark
  • 993
  • 1
  • 6
  • 5

10 Answers10

88

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];
  }
}
Lineous
  • 1,642
  • 8
  • 8
57

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
}
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
25

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();
  }
gmdev
  • 2,725
  • 2
  • 13
  • 28
8

A simple example, where it shows how we can resolve the above issue, Example: Create an instance of class B, and pass an instance of class A in the parameter of it

WRONG(Compile time error of initializer):

  final A _a = A();
  final B _b = B(_a);

shows error: The instance member '_a' can't be accessed in an initializer.


Right:

  final A _a = A();
  late final B _b;

  AppointmentRepository() {
    _b = B(_a);
  }
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
3

#100% working solution : Juts place var myTest = params.[comLevel]; below your Build method.

eg.

class LevelUp extends GetxController {
   Map<String, String> params = Get.arguments;
    
   Widget build(BuildContext context) {
  var myTest = params.[comLevel];
}
}
ouflak
  • 2,458
  • 10
  • 44
  • 49
  • I told the same thing but got downvoted for God knows why. :D Look at all those answers. People like making things complicated. – zibidigonzales Aug 05 '22 at 09:54
2

For me it happened Because i was trying to access a Property of a class instance (Lets Say Class A ) And Use this property to initialize Another Class (Class B) , The Property Was Integer Number and Was Defined

However , Since i didn't Make an Object from "Class A" I can access those propertied Belong to it ! I tried to use this property inside the "Build" Method so that an object is "Created/Built" And it Worked !

2

You can't use instance members as a initialization for a variable in a class. If you can make the member static, that is the simplest and safest way to solve the problem. It is introduced in Dart Document as well: Link

Solution:

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

  var myTest = params.[comLevel];
}
Minki Jung
  • 75
  • 1
  • 6
1

I also got the similar error. And I found the solution as follows.

My first code:

final BuildContext mycontext = GlobalContextClass.navigatorKey.currentContext;
final PsValueHolder psValueHolder = Provider.of<PsValueHolder>(mycontext, listen: false);

Next is the code where the error is fixed:

final PsValueHolder psValueHolder = Provider.of<PsValueHolder>(GlobalContextClass.navigatorKey.currentContext, listen: false);

Instead of defining 2 variables in a row, I placed the first variable directly in the place of the 2nd variable.

smebes
  • 241
  • 3
  • 2
1

Another solution is making your variable, a GetX parameter.

  int count_myProducts = cartItems.length; //The instance member 'cartItems' can't be accessed in an initializer. (Documentation)

  int get count_myProducts => cartItems.length; 

see this video at 27:34 GetX State Management tutorial with Flutter https://www.youtube.com/watch?v=ZnevdXDH25Q&ab_channel=CodeX

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Saad Mansoor
  • 199
  • 7
-2

Just carry

var myTest = params.[comLevel];

into Widget build{} below.

like this :

class LevelUp extends GetxController {
   Map<String, String> params = Get.arguments;
    
   Widget build(BuildContext context) {
     var myTest = params.[comLevel];
   }
}
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
zibidigonzales
  • 338
  • 3
  • 9