flutter - 位置参数太多,预期为 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.
解决方案
这里的问题是您为您的类使用命名参数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"]);
推荐阅读
- python - 从几个列表列表创建数据框
- php - 如何修复以下 PHP 警告?
- vuejs2 - 如何制作无限嵌套循环
- php - Laravel如何将图像上传到数据库并在视图中显示
- python - 通过给定的行索引对 pandas DataFrame 进行分组
- flutter - 如何调整文件大小然后转换为base64?
- java - 试图保存一个 HashMap
- 在 h2 数据库中由于“值太长 ... BINARY(255)”而失败
- django - “str”的 Pylint Django 模型实例没有成员
- php - Slim 3 框架 - 将数据从中间件传递到控制器 - 动作参数
- spring - 如何在百里香中设置变量?