我尝试在 flutter material 应用程序中为玩家创建模型。我创建了一个名为“player”的类并实现了它。我编辑了用于连接 firebase 的 gradle 文件。我的代码中出现了一些错误。所以请帮我解决这个错误。
错误
Error: The argument type 'Object Function()' can't be assigned to the parameter type 'Map<String, dynamic>'.
- 'Object' is from 'dart:core'.
- 'Map' is from 'dart:core'.
: this.fromMap(snapshot.data, documentReference: snapshot.reference);
^
FAILURE: Build failed with an exception.
* Where:
Script 'C:\src\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1035
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 11m 3s
Exception: Gradle task assembleDebug failed with exit code 1
这是模型类。这里我添加了一些玩家的属性。
模型类
import 'package:cloud_firestore/cloud_firestore.dart';
class player {
String bio;
String name;
String city;
String age;
String role;
String runs;
String avg;
String wiki;
DocumentReference documentReference;
player(
{this.bio,
this.name,
this.city,
this.age,
this.role,
this.runs,
this.avg,
this.wiki});
player.fromMap(Map<String, dynamic> map, {this.documentReference}) {
bio = map["bio"];
name = map["name"];
city = map["city"];
age = map["age"];
role = map["role"];
runs = map["runs"];
avg = map["avg"];
wiki = map["wiki"];
}
player.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, documentReference: snapshot.reference);
toJson() {
return {
'bio': bio,
'name': name,
'city': city,
'age': age,
'role': role,
'runs': runs,
'avg': avg,
'wiki': wiki
};
}
}
答案 0 :(得分:0)
使用 snapshot.data()
而不是 snapshot.data
。 migration guide 中提到了这一点。
突破:现在通过 data() 方法通过数据获取器获取快照数据。