close
The instance member 'x' can't be accessed in an initializer, instance member 'x' can't be accessed in an initializer, accessed in an initializer error, move initialization into a constructor, class initialized the object

[Solved] The instance member ‘x’ can’t be accessed in an initializer.

Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. The instance member ‘x’ can’t be accessed in an initializer in a flutter. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How The instance member ‘x’ can’t be accessed in an initializer Error Occurs ?

I have one class and whenever that class build I want to initialized the object. Here is my code. And I am using GetX.

class ProductReview extends GetxController {
  Map<String, String> productReviews = Get.reviews;

  var avg_rating = productReviews.[avg_rating];
}

But this code give me following error.

Error report--"The instance member 'productReviews' 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 avg_rating. It doesn't matter if I put the key in quotes or provide an integer. Any advice would be greatly appreciated.

How to Solve The instance member ‘x’ can’t be accessed in an initializer Error?

  1. How to Solve The instance member 'x' can't be accessed in an initializer Error?

    To Solve The instance member 'x' can't be accessed in an initializer you just have to move avg_rating initialization into a constructor. Here we can't direct assign productReviews before Class initialized the object.

  2. The instance member 'x' can't be accessed in an initializer

    To Solve The instance member 'x' can't be accessed in an initializer you just have to move avg_rating initialization into a constructor. Here we can't direct assign productReviews before Class initialized the object.

Solution 1

Here we can’t direct assign productReviews before Class initialized the object. To fix you just move avg_rating initialization into a constructor. Here is how you can do.

class ProductReview extends GetxController {
  Map<String, String> productReviews = Get.reviews;
  String avg_rating;
  
  ProductReview() {
    avg_rating = productReviews[avg_rating];
  }
}

Summery

So, It’s All About This Error. I hope this tutorial helps you to Solve your error. Please Comment Below if You stucks anywhere with my code. And please comment below on which solution worked for you. Thank You.

Also Check Out Below Tutorials


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *