gpt4 book ai didi

flutter 2.0 : How to create a model class for firestore snapshot

转载 作者:行者123 更新时间:2023-12-04 14:55:17 26 4
gpt4 key购买 nike

如何为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>";
}
但是有一个错误说需要 3 个位置参数,但找到了 1 个。
任何帮助将不胜感激。

最佳答案

你没有通过 piddescription (这是位置参数)。您可以使它们成为可选的命名参数或显式传递它们。
解决方案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>';
}
方案二(传参数):
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>';
}

关于 flutter 2.0 : How to create a model class for firestore snapshot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68178708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com