gpt4 book ai didi

firebase - 在 Flutter 中添加记录时,Cloud Firestore 无法正确更新

转载 作者:行者123 更新时间:2023-12-03 02:47:56 26 4
gpt4 key购买 nike

我正在开发一个 Flutter 应用程序,我正在使用 cloud_firestore 插件。我有一个提交的集合,我正在使用 StreamBuilder 来显示它们(我假设它会在流更改时更新)。我从字面上从插件示例中获取了示例,因为没有太多关于如何使用插件执行操作的文档。当我添加一条记录时,我显示的文档列表变长了,但它似乎是在复制其中一个提交,而不是插入新的提交。添加后新提交不显示。这是我如何显示列表的代码:

// At the top of the class home.dart. 
final submissions = Firestore.instance.collection('submissions');

// This is in submission-list.dart and the above submissions
// is passed in to the contructor
Widget build(BuildContext context) {
return new StreamBuilder<QuerySnapshot>(
stream: submissions
.where('owner_uid', isEqualTo: this.user.uid)
.orderBy('timestamp', descending: true)
.snapshots,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
var date = _formatDate(document['timestamp']);
String body = _constructCardBody(document['weight'],
bodyFat: document['bodyFat']);
String id = document.documentID;
return new SubmissionCard(id: id, title: date, body: body, submissions: submissions);
}).toList(),
);
},
);
}

这里是完整的 submission-card.dart:

import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

import '../utils/logger.dart';
import './block-button.dart';

class SubmissionCard extends StatefulWidget {
final String id;
final String title;
final String body;
final CollectionReference submissions;

SubmissionCard({this.id, this.title, this.body, this.submissions});

@override
State<StatefulWidget> createState() =>
new _SubmissionCardState(id: this.id, title: this.title, body: this.body, submissions: this.submissions);
}

class _SubmissionCardState extends State<SubmissionCard> {
final String id;
final String title;
final String body;
bool showActionButtons = false;
final CollectionReference submissions;

_SubmissionCardState({this.id, this.title, this.body, this.submissions});

void _showEditScreen() {}

void _showActionButtons() {
setState(() {
showActionButtons = true;
});
}

void _hideActionButtons() {
setState(() {
showActionButtons = false;
});
}

Future<Null> _deleteSubmission() async {
try {
await submissions.document(id).delete();
await Logger.log('error', 'stackTrace');
} catch (error, stackTrace) {
await Logger.log(error, stackTrace);
}
}

void _closeDialog() {
Navigator.of(context).pop();
_hideActionButtons();
}

Future<Null> _warnAboutDeletion() async {
return showDialog(
context: context,
child: new SimpleDialog(
title: new Text('Are you sure?'),
children: <Widget>[
new SimpleDialogOption(
onPressed: () {
this._deleteSubmission();
this._closeDialog();
},
child: new Text("I'm sure. Delete it."),
),
new SimpleDialogOption(
onPressed: _closeDialog,
child: new Text("Nope. Take me back."),
),
],
)
);
}

@override
Widget build(BuildContext context) {
return new GestureDetector(
onLongPress: _showActionButtons,
onTap: _hideActionButtons,
child: new Card(
elevation: showActionButtons ? 8.0 : 2.0,
key: new GlobalKey(),
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new ListTile(
trailing: showActionButtons
? new Row(
children: <Widget>[
new IconButton(
padding: const EdgeInsets.all(0.0),
icon: const Icon(Icons.edit),
onPressed: _showEditScreen,
color: Colors.black12,
splashColor: Colors.black26,
highlightColor: Colors.black12,
),
new IconButton(
padding: const EdgeInsets.all(0.0),
icon: const Icon(Icons.delete),
onPressed: _warnAboutDeletion,
color: Colors.redAccent,
splashColor: Colors.black26,
highlightColor: Colors.black12,
),
],
)
: new Container(),
isThreeLine: true,
title: new Text(title),
subtitle: new Text(
body,
style: new TextStyle(height: 3.0),
),
),
],
),
),
);
}
}

repo 链接:https://github.com/dericgw/bodwatch

以前,当我使用 Firebase 时,这个集合会自动更新。我以前从未见过这种奇怪的行为。现在,我是 Flutter 和 Dart 的新手,所以我肯定会遗漏一些东西。

最佳答案

您需要在 firebase 控制台中添加索引。

在您的情况下,您需要多个索引。1. owner_uid,升序2.时间戳,降序

问题应该解决了。

关于firebase - 在 Flutter 中添加记录时,Cloud Firestore 无法正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48589274/

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