首页 > 解决方案 > 我的时间戳没有启用,它显示是错误的

问题描述

我只想使用 timeago 包,为此我试图通过编写以下代码来启用时间戳,但它在编写时显示错误(下面的红线)。另外我正在使用 timeago:^2.0.26 和 cloud_firestore:^0.13 .7.

void main() {
  Firestore.instance.settings(timestampsInSnapshotsEnabled: true).then((_) {
    print("Timestamps enabled in snapshots\n");
  }, onError: (_) {
    print("Error enabling timestamps in snapshots\n");
  });
  runApp(MyApp());
}

标签: firebasefluttergoogle-cloud-firestorecross-platform

解决方案


 Firestore.instance.settings(timestampsInSnapshotsEnabled: true).then((_)

这种启用时间戳的方式现在已被弃用,并且不需要该参数,因为默认情况下启用了时间戳。

但是您必须明确告诉程序您正在等待一些数据或在主函数中运行异步函数。代码如下:

 WidgetsFlutterBinding.ensureInitialized();

所以整个代码看起来像这样

void main() {

  WidgetsFlutterBinding.ensureInitialized(); //The reason behind this is I am waiting for some data or running an async function inside main().
  
  Firestore.instance.settings().then((_) {
    print("Timestamps enabled in snapshots\n");
  }, onError: (_) {
    print("there was an error enabling timestams in snapshots");
  });
  runApp(MyApp());
}

推荐阅读