Home > database >  The class I specified for the item delete button from the list in Dart Flutter gives an error
The class I specified for the item delete button from the list in Dart Flutter gives an error

Time:01-27

I'm trying to make a list with Dart Flutter. I added a button and when I click the button, the item will be deleted from the list. But I need to assign an id to each item in the list. In education, it does not give an error in classes, but it does for me.

class Kisiler {
  int id;
  String ad;
  String soyad;
  int numara;

  Kisiler.withId(int id, String ad, String soyad, int numara) {
    this.id = id;
    this.ad = ad;
    this.soyad = soyad;
    this.numara = numara;
  }

  Kisiler(String ad, String soyad, int numara) {
    this.id = id;
    this.ad = ad;
    this.soyad = soyad;
    this.numara = numara;
  }
}

While I do: enter image description here

When training:

enter image description here

What is the problem? How can I solve it? I'm creating a list on main.dart with the following code:

CodePudding user response:

That is because of null safety.

class Kisiler {
  int? id;
  late String ad;
  late String soyad;
  late int numara;

  Kisiler.withId(this.id, this.ad, this.soyad, this.numara);

  Kisiler(this.ad, this.soyad, this.numara);
} 

CodePudding user response:

This is due to null safety error.

Initialize the members as late

  late int id;
  late String ad;
  late String soyad;
  late int numara;

CodePudding user response:

You can do this way also by using late keyword:

class Kisiler {
   late  int id;
   late String ad;
   late String soyad;
   late int numara;

  Kisiler.withId(this.id, this.ad, this.soyad, this.numara);

  Kisiler(this.ad, this.soyad, this.numara);
} 
  •  Tags:  
  • Related