微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter - 从 Firestore 获取数据并在应用启动时保存到 Provider

如何解决Flutter - 从 Firestore 获取数据并在应用启动时保存到 Provider

我需要从 Firestore 获取一些数据,并在应用首次打开时将其保存到 Provider。

这是加载的第一个页面。这是一个有状态的小部件,我正在尝试像这样获取数据:

@override
  void initState() {
    super.initState();
    final User user = Provider.of<User>(context,listen: false);
    _getItems(user);
  }

  _getItems(user) {
    final content = Provider.of<Content>(context,listen: false);
    //get data from Firestore using user.uid
    //save it to content.ids
    content.ids = {"1": 2};
  }

执行此操作时,我收到以下错误

Flutter: The following assertion was thrown while dispatching notifications for Content:
Flutter: setState() or markNeedsBuild() called during build.
Flutter: This _InheritedProviderScope<Content> widget cannot be marked as needing to build because the
Flutter: framework is already in the process of building widgets.  A widget can be marked as needing to be
Flutter: built during the build phase only if one of its ancestors is currently building. This exception is
Flutter: allowed because the framework builds parent widgets before children,which means a dirty descendant
Flutter: will always be built. Otherwise,the framework might not visit this widget during this build phase.
Flutter: The widget on which setState() or markNeedsBuild() was called was:
Flutter:   _InheritedProviderScope<Content>

在应用程序启动时获取数据并保存到 Provider 的最佳方式是什么?我知道我在 MultiProvider 中有 main.dart 选项,但我在那里没有用户 ID。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。