Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too many positional arguments, 0 expected, but 3 found

Tags:

flutter

dart

I made a class notification to initialise my data, but when I call it in my future function it shows me an error.

Future <List<Notification>> _getNotification() async {
    var data = await http.get("$baseUrl/efficience001_webservice/notification.php");
    var jsonData = json.decode(data.body);
    List<Notification> notifs = [];
    for (var u in jsonData){
      Notification notif = Notification(u["description"],u["nom_ligne"], u["created_at"]);
      notifs.add(notif);
    }
    print(notifs.length);
    return notifs;
}

class Notification {
  final String description;
  final String nom_ligne;
  final DateTime created_at;

  const Notification({this.description, this.nom_ligne, this.created_at});
}
error: Too many positional arguments , 0 expected, but 3 found.
like image 980
Oussama Ridéne Avatar asked Jun 21 '19 05:06

Oussama Ridéne


1 Answers

The problem here is that you are using named parameters for your Notification class but pass positional parameters.

You have two options to solve it:

  • Use positional parameters for your class (remove curly braces). In order to do that, change your constructor in the Notification class to:
const Notification(this.description, this.nom_ligne, this.created_at);
  • Supply named parameters when creating the object of your Notification class. In order to do this, change one line in your for-loop:
Notification notif =
  Notification(description: u["description"], nom_ligne: u["nom_ligne"], created_at: u["created_at"]);
like image 79
creativecreatorormaybenot Avatar answered Nov 15 '22 10:11

creativecreatorormaybenot