flutter - The instance member 'params' can't be accessed in an initializer - TagMerge
5The instance member 'params' can't be accessed in an initializerThe instance member 'params' can't be accessed in an initializer

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

Asked 1 years ago
43
5 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];
  }
}

Source: link

15

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();
  }

Source: link

4

Before Null Safety:

You couldn't access instance members (unless they were declared static) before the class was instantiated, hence the following code has error.

class Test {
  int foo = 0;
  int bar = foo; // Error
}

After Null safety:

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; // OK, no error
}

Source: link

0

[Solved] The instance member ‘x’ can’t be accessed in an initializer. September 20, 2021June 24, 2021 by Milan Dhameliya
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.
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];
  }
}

Source: link

0

A class_declaration is a type_declaration (Type declarations) that declares a new class.
class_declaration
    : attributes? class_modifier* 'partial'? 'class' identifier type_parameter_list?
      class_base? type_parameter_constraints_clause* class_body ';'?
    ;
A class_declaration may optionally include a sequence of class modifiers:
class_modifier
    : 'new'
    | 'public'
    | 'protected'
    | 'internal'
    | 'private'
    | 'abstract'
    | 'sealed'
    | 'static'
    | class_modifier_unsafe
    ;
When a non-abstract class is derived from an abstract class, the non-abstract class must include actual implementations of all inherited abstract members, thereby overriding those abstract members. In the example
abstract class A
{
    public abstract void F();
}

abstract class B: A
{
    public void G() {}
}

class C: B
{
    public override void F() {
        // actual implementation of F
    }
}
A type parameter is a simple identifier that denotes a placeholder for a type argument supplied to create a constructed type. A type parameter is a formal placeholder for a type that will be supplied later. By contrast, a type argument (Type arguments) is the actual type that is substituted for the type parameter when a constructed type is created.
type_parameter_list
    : '<' type_parameters '>'
    ;

type_parameters
    : attributes? type_parameter
    | type_parameters ',' attributes? type_parameter
    ;

type_parameter
    : identifier
    ;
A class declaration may include a class_base specification, which defines the direct base class of the class and the interfaces (Interfaces) directly implemented by the class.
class_base
    : ':' class_type
    | ':' interface_type_list
    | ':' class_type ',' interface_type_list
    ;

interface_type_list
    : interface_type (',' interface_type)*
    ;

Source: link

Recent Questions on flutter

    Programming Languages