首页 > 解决方案 > 位置参数太多,预期为 0,但找到了 3 个

问题描述

我做了一个类通知来初始化我的数据,但是当我在未来的函数中调用它时,它会显示一个错误。

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.

标签: flutterdart

解决方案


这里的问题是您为您的类使用命名参数Notification但传递位置参数。

您有两种选择来解决它:

  • 为您的班级使用位置参数(删除花括号)。为此,请将Notification类中的构造函数更改为:
const Notification(this.description, this.nom_ligne, this.created_at);
  • 在创建Notification类的对象时提供命名参数。为此,请更改for-loop 中的一行:
Notification notif =
  Notification(description: u["description"], nom_ligne: u["nom_ligne"], created_at: u["created_at"]);

推荐阅读