Dar*_*ong 5 flutter google-cloud-firestore
如何为 flutter 2.0 的 firestore 快照构建 flutter 类
我尝试过这样的事情:
import 'package:cloud_firestore/cloud_firestore.dart';
class Post {
final String pid;
final String description;
final DocumentReference reference;
Post.fromMap(Map<String, dynamic> map, this.pid, this.description, {required this.reference})
Post.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data(), reference: snapshot.reference);
@override
String toString() => "Post<$pid:$description>";
}
Run Code Online (Sandbox Code Playgroud)
但出现错误,指出需要 3 个位置参数,但找到了 1 个。
您没有传递pid和description(这是位置参数)。您可以将它们设为可选的命名参数或显式传递它们。
解决方案1(使参数可选):
class Post {
final String pid;
final String description;
final DocumentReference reference;
Post.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(
snapshot.data() as Map<String, dynamic>,
reference: snapshot.reference,
);
Post.fromMap(
Map<String, dynamic> map, {
required this.reference,
this.pid = 'dummyID',
this.description = 'dummyDesc',
});
@override
String toString() => 'Post<$pid:$description>';
}
Run Code Online (Sandbox Code Playgroud)
解决方案2(传递参数):
class Post {
final String pid;
final String description;
final DocumentReference reference;
Post.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(
snapshot.data() as Map<String, dynamic>,
'pid',
'description',
reference: snapshot.reference,
);
Post.fromMap(
Map<String, dynamic> map,
this.pid,
this.description, {
required this.reference,
});
@override
String toString() => 'Post<$pid:$description>';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1031 次 |
| 最近记录: |