【问题标题】:Too many positional arguments, 0 expected, but 3 found位置参数太多,预期为 0,但找到了 3 个
【发布时间】:2019-11-03 22:56:25
【问题描述】:

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

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.

【问题讨论】:

标签: flutter dart


【解决方案1】:

这里的问题是您在 Notification 类中使用了 named parameters,但传递了位置参数。

你有两种选择来解决它:

  • 为您的类使用位置参数(删除花括号)。为此,请将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"]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2022-07-20
    • 2021-11-09
    • 2020-04-20
    • 2021-06-14
    • 2021-11-10
    • 2021-05-22
    • 2020-06-15
    相关资源
    最近更新 更多