Google cloud firestore 如何使用flatter配置Firebase Firestore设置
我正在评估这个问题 此代码段按预期工作,当按下中的浮动按钮时,它会将一条记录插入firestore:Google cloud firestore 如何使用flatter配置Firebase Firestore设置,google-cloud-firestore,flutter,Google Cloud Firestore,Flutter,我正在评估这个问题 此代码段按预期工作,当按下中的浮动按钮时,它会将一条记录插入firestore: import 'package:flutter/material.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Widget buil
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Firestore firestore;
_MyHomePageState() {
firestore = Firestore.instance;
}
void _addBoard() {
setState(() {
_counter++;
firestore.collection("boards").document().setData({ 'name': 'mote_$_counter', 'descr': 'long descr $_counter' });
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _addBoard,
child: new Icon(Icons.add),
),
}
}
有一种方法可以配置FirebaseFirestore实例,从而通过日期设置解决此问题?应该在版本0.8.2中解决
有关如何使用设置的详细信息,请参见。问题已从cloud_firestore 0.8.2中解决,首先将
pubspec.yaml
中的依赖项升级为:
dependencies:
cloud_firestore: ^0.8.2+1
返回Future,因此必须在异步函数中调用它
这足以使works成为示例代码:
void main() async {
final Firestore firestore = Firestore();
await firestore.settings(timestampsInSnapshotsEnabled: true);
runApp(new MyApp());
}
然后,在实际添加文档时,您显式地添加了一个createdAt
(或任何您想称之为的)字段,如下所示:
Firestore.instance
.collection('customer')
.add({
"name": customerNameController.text,
"createdAt": FieldValue.serverTimestamp()
}).then((res) { ...
我也面临着同样的问题,GitHub中有一个问题打开了,但从未接近,所以我不喜欢这样的声音,但我一直在等待这个问题的任何进展@attdona-你能用
wait firestore.settings(timestampsInSnapshotsEnabled:true)展示一下你是如何修改现有代码的吗代码>东西。我非常怀疑如何使用它。在这里-在使用部分没有提到关于wait firestore.settings(timestampsInSnapshotsEnabled:true)的内容代码>东西。所以,我有点搞不清楚该用什么。链接已经关闭了。请将解决方案放在这里,而不是提供其他页面的链接。
Firestore.instance
.collection('customer')
.add({
"name": customerNameController.text,
"createdAt": FieldValue.serverTimestamp()
}).then((res) { ...