这最初可以正常工作,但是在更新Firebase之后,现在却给了我这个错误。我在给出错误的部分添加了星号。错误消息已添加到代码下方。
import 'package:cloud_firestore/cloud_firestore.dart';
class Record {
final String name;
final int totalVotes;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['name'] != null),
assert(map['totalVotes'] != null),
name = map['name'],
totalVotes = map['totalVotes'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(**snapshot.data**, reference: snapshot.reference);
@override
String toString() => "Record<$name:$totalVotes>";
}
class Voters {
String uid;
String voteId;
String markedVoteOption;
}
参数类型'Map
答案 0 :(得分:10)
尝试以下操作:
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data(), reference: snapshot.reference);
data()
是一种方法:
/// Contains all the data of this [DocumentSnapshot].
Map<String, dynamic> data() {
return _CodecUtility.replaceDelegatesWithValueInMap(
_delegate.data(), _firestore);
}
返回一个Map<String, dynamic>
答案 1 :(得分:1)
这是因为DocumentSnapshot.data()是一个返回Map<String, dynamic>
的函数。
因此,答案是:
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data(), reference: snapshot.reference);
答案 2 :(得分:0)
这就是我解决的方法。
import 'package:cloud_firestore/cloud_firestore.dart';
class Record {
final String name;
final int totalVotes;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map(), {this.reference})
: assert(map()['name'] != null),
assert(map()['totalVotes'] != null),
name = map()['name'],
totalVotes = map()['totalVotes'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$name:$totalVotes>";
}
class Voters {
String uid;
String voteId;
String markedVoteOption;
}
/// Contains all the data of this [DocumentSnapshot].
Map<String, dynamic> data() {
return _CodecUtility.replaceDelegatesWithValueInMap(
_delegate.data(), _firestore);
}
答案 3 :(得分:0)
显然,所有需要的只是地图上的括号
import 'package:cloud_firestore/cloud_firestore.dart';
class Record {
final String name;
final int votes;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map(), {this.reference})
: assert(map()['name'] != null),
assert(map()['votes'] != null),
name = map()['name'],
votes = map()['votes'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
@override
String toString() => "Record<$name:$votes>";
}