如何使用 Flutter 配置 Firebase Firestore 设置
Posted
技术标签:
【中文标题】如何使用 Flutter 配置 Firebase Firestore 设置【英文标题】:How to Configure Firebase Firestore settings with Flutter 【发布时间】:2018-12-09 07:28:39 【问题描述】:我正在评估Firestore Plugin for Flutter。
这个 sn-p 按预期工作,它在按下浮动按钮时将一条记录插入到 firestore:
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),
),
使用flutter run
运行控制台显示警告:
W/Firestore(31567): (0.6.6-dev) [Firestore]: The behavior for java.util.Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK.
W/Firestore(31567): To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
W/Firestore(31567):
W/Firestore(31567): FirebaseFirestore firestore = FirebaseFirestore.getInstance();
W/Firestore(31567): FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
W/Firestore(31567): .setTimestampsInSnapshotsEnabled(true)
W/Firestore(31567): .build();
W/Firestore(31567): firestore.setFirestoreSettings(settings);
W/Firestore(31567):
W/Firestore(31567): With this change, timestamps stored in Cloud Firestore will be read back as com.google.firebase.Timestamp objects instead of as system java.util.Date objects. So you will also need to update code expecting a java.util.Date to instead expect a Timest
有一种方法可以配置 FirebaseFirestore 实例,从而通过日期设置解决此问题?
【问题讨论】:
我面临同样的问题,GitHub 中有一个问题已打开但从未关闭,所以我不喜欢这样的声音,但我一直在等待这个问题的任何进展@ 987654322@ @attdona - 你能展示一下你是如何使用await firestore.settings(timestampsInSnapshotsEnabled: true);
来修改现有代码的吗?我很怀疑如何使用它。
【参考方案1】:
从cloud_firestore 0.8.2解决问题,先将pubspec.yaml
中的依赖升级为:
dependencies:
cloud_firestore: ^0.8.2+1
Firestore::setting
返回一个 Future,因此必须在异步函数中调用它。
这足以使示例代码工作:
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) ...
【讨论】:
【参考方案2】:应该在 0.8.2 版本中解决。
请参阅example 了解如何使用设置。
【讨论】:
在这里 - github.com/flutter/plugins/tree/master/packages/cloud_firestore 在使用部分没有提到await firestore.settings(timestampsInSnapshotsEnabled: true);
的事情。所以,我对使用什么感到有点困惑。
链接已关闭。请将解决方案放在这里,而不是提供其他页面的链接。以上是关于如何使用 Flutter 配置 Firebase Firestore 设置的主要内容,如果未能解决你的问题,请参考以下文章
Flutter + firebase:配置应用程序以使用本地 firebase 模拟器
如何在 Flutter Web 项目中设置 Firebase 功能
尝试使用 Firebase 远程配置运行 Flutter WEB 应用程序时出错 [关闭]