我正在尝试上载多个文本字段的值,但是firestore会继续重置所有值,以便旧值消失。我希望能够为第一个文本字段(alOne)设置一个值,也许以后再回来为第二个文本字段(alTwo)设置一个值。但是在我的情况下,当设置了第二个值时,第一个从firestore中消失了吗?
这是我的帖子功能
static void createPostMonday(Post post) async{
await postsRef.document(post.authorId).setData({
'alOne':post.alOne,
'alTwo':post.alTwo,
'alThree':post.alThree,
'alFour':post.alFour,
'alFive':post.alFive,
'alSix':post.alSix,
'beOne':post.beOne,
'beTwo':post.beTwo,
'beThree':post.beThree,
'beFour':post.beFour,
'beFive':post.beFive,
'beSix':post.beSix,
'likes': post.likes,
'authorId': post.authorId,
'timestamp': post.timestamp,
},merge: true);
这是操作(提交)按钮
_submit() async{
Post post = Post(
authorId: Provider.of<UserData>(context, listen: false).currentUserId,
timestamp: Timestamp.fromDate(DateTime.now()),
alOne1: _alOne1,
alTwo1: _alTwo1,
alThree1: _alThree1,
alFour1: _alFour1,
alFive1: _alFive1,
alSix1: _alSix1,
beOne1: _beOne1,
beTwo1: _beTwo1,
beThree1: _beThree1,
beFour1: _beFour1,
beFive1: _beFive1,
beSix1: _beSix1,
);
DatabaseService.createPostMonday(post);
Navigator.pop(context);
}
这是我的文本字段中的2。
Padding(
padding: EdgeInsets.symmetric(horizontal: 30.0),
child: TextField(
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Alternativ',
),
onChanged: (input) => _alOne1 = input,
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 30.0),
child: TextField(
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Beskrivning',
),
onChanged: (input) => _beOne1 = input,
),
),
您正在调用setData
,它将用您传递的数据替换文档中的所有现有数据。
如果要合并传入的数据与文档中已有的任何现有数据,则有两个选择:
使用updateData
method,它将使用您提供的数据更新文档。如果文档尚不存在,则此写操作将失败。
将updateData
作为第二个参数传递给{ merge: true }
,在这种情况下,数据将被合并,如果尚不存在,则将创建该文档。
我通常使用第二种方法,因为我通常不知道/不在乎文档是否已经存在。