【问题标题】:How to use future functions properly如何正确使用未来的功能
【发布时间】:2020-01-05 10:20:55
【问题描述】:

我有一个 stremBuilder 从 firebase 获取快照,该快照包含一些帖子信息,我使用它来创建 Posting 类的新实例,并将此帖子传递给 postingPage

Posting 类的属性之一是上传帖子的用户的 ID。

我有一个基于 id 获取用户的函数,但我不知道在哪里调用它,因为它是一个 Future 函数。

我需要在哪里调用这个获取用户函数来获取postingPage中的所有用户数据?

代码预览:

StreamBuilder<QuerySnapshot>(
  stream: _stream,
  builder: (
    BuildContext context,
    AsyncSnapshot<QuerySnapshot> snapshots
  ) {
    if (snapshots.hasError) {
      print(snapshots.error.toString());
    }
    switch (snapshots.connectionState) {
      case ConnectionState.waiting:
        return Center(child: CircularProgressIndicator());
      default:
        return GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            childAspectRatio: 3 / 4
          ),
          itemCount: snapshots.data.documents.length,
          itemBuilder: (context, index) {
            DocumentSnapshot snapshot =
              snapshots.data.documents[index];
            Posting currentPosting =
              posting.getPostingData(snapshot);
            return InkWell(
              onTap: () {
                // Navigator.of(context)
                //     .pushNamed(ViewPostingScrenn.routeName);
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => ViewPostingScrenn(
                      posting: currentPosting,
                    ),
                  ),
                );
              },
              child: PostingGridTile()
            );
          },
        );
    }
  },
),

【问题讨论】:

标签: flutter dart


【解决方案1】:

您可以为您的网络获取链接期货并在您的小部件中使用FutureBuilder 而不是StreamBuilder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2020-12-13
    • 2013-03-30
    • 1970-01-01
    相关资源
    最近更新 更多