Dart error, This constructor should initialize field 'x' because its type 'int' doesn't allow null

Just a learner

I'm new to Dart. This is my code:

class Point {
  int x;
  int y;

  Point(this.x, this.y);

  Point.same(int i) {
    x = i;
    y = i;
  }
}

main() {}

I got this error when running it.

bin/dart1.dart:7:3: Error: This constructor should initialize field 'x' because its type 'int' doesn't allow null.
  Point.same(int i) {
  ^
bin/dart1.dart:2:7: Context: 'x' is defined here.
  int x;
      ^
bin/dart1.dart:7:3: Error: This constructor should initialize field 'y' because its type 'int' doesn't allow null.
  Point.same(int i) {
  ^
bin/dart1.dart:3:7: Context: 'y' is defined here.
  int y;
      ^

As I understand, the parameter int i of Point.same() is non-nullable, and I use it to initialize x and y (x = i; y = i;). Why it still asks me to initialize x and y? I even tried the following code and still got the same error:

class Point {
  int x;
  int y;

  Point(this.x, this.y);

  Point.same() {
    x = 3;
    y = 3;
  }
}

main() {}
Christopher Moore

When initializing fields from a constructor without constructor parameters, use the initializer list. It runs prior to the constructor body and allow you to initialize final and non-nullable variables without marking them late.

class Point {
  int x;
  int y;

  Point(this.x, this.y);

  Point.same(int i) :
    x = i,
    y = i;
}

main() {}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error: Field '_connectionChangeStream' should be initialized because its type 'StreamSubscription<dynamic>' doesn't allow null

Error on VS Code but not on Dartpad. - Field 'percent' should be initialized because its type 'double' doesn't allow null

Field '_suratList' should be initialized because its type 'Future<List<QuranSurat>>' doesn't allow null

Field '_areas' should be initialized because its type 'List<Area>' doesn't allow null

The parameter can't have a value of 'null' because of its type in Dart

Constructor doesn't satisfy null safety in Dart

Overriden method, that is invoked from parent constructor, doesn't initialize a field

Error: A non-null value must be returned since the return type 'Widget' doesn't allow null

@require parameter '#' can't have a value of 'null' because of its type

The parameter 'id' can't have a value of 'null' because of its type?

Generic constructor allow int type

Dart: The type of function literal can't be inferred because the literal has a block as its body

The Flutter Error show that " A non-null value must be returned since the return type 'Widget' doesn't allow null "

Initialize php object with arguments, while its constructor doesn't take any (singleton pattern)

The parameter 'chatmessage' can't have a value of 'null' because of its type, but the implicit default value is 'null'

The parameter 'colour' can't have a value of 'null' because of its type, but the implicit default value is 'null'

The parameter 'builder' can't have a value of 'null' because of its type, but the implicit default value is 'null'

The parameter 'padding' can't have a value of 'null' because of its type, but the implicit default value is 'null'

Why does my constructor not initialize its superclass attributes like it should?

Why private variable doesn't initialize in dart?

Compiler error because constructor must explicitly initialize the member

Type Error: Sum doesn't allow distinct (Django)

Constructor doesn't initialize proper values

This code throws an error at line 6. Is it because cout stream doesn't allow it or it's some conflict in ostream?

Flutter Dart Firebase error: The function can't be unconditionally invoked because it can be 'null'

Can't initialize final property inside Dart constructor

Type error doesn't show up when it should

Error: "...because type variable ‘t’ would escape its scope" using package "servant" in Haskell

Join DefaultIfEmpty() Error: The cast to value type 'System.Int32' failed because the materialized value is null