显示未来列表功能firebase getx

发布于 2025-02-02 23:53:25 字数 1796 浏览 5 评论 0原文

我正在尝试创建一个用户供稿,就像使用Firebase&的Twitter一样。 getx。
在代码段中是我的函数。

List<PostModel> postListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
  return PostModel(
    id: doc.id,
    text: (doc.data() as dynamic)["text"] ?? "",
    creator: (doc.data() as dynamic)["creator"] ?? "",
    timestamp: (doc.data() as dynamic)["timestamp"] ?? 0,
  );
}).toList();
}

Future<List<PostModel>> getFeed() async {
List<String> usersFollowing = await UserService() //['uid1', 'uid2']
    .getUserFollowing(FirebaseAuth.instance.currentUser!.uid);

QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("posts").where('creator', whereIn: usersFollowing)
.orderBy('timestamp', descending: true)
.get();

return postListFromSnapshot(querySnapshot);
}

我想做的是显示未来函数getFeed(),我正在使用getX进行状态管理。因此,我的问题是如何使用listView.builder()显示此函数的结果

,这是我使用未来构建器的方式

FutureBuilder(
  future: _.listPost,
  initialData: [PostModel(id: "2", creator: "Fm", text: "Testing", timestamp: Timestamp.now())],
  builder: (BuildContext context, AsyncSnapshot snapshot){
    if(snapshot.hasData == null){
      return Text("Data is available");
    } else{
      return ListView.builder(
        shrinkWrap: true,
        itemCount: snapshot.data.toString().length,
        itemBuilder: (context, index){
          PostModel posts = snapshot.data[index];
          return Column(
            children: [
              Text(posts.text)
            ],
          );
        },
      );
    }
  },
)

,这是我遇到的错误

The following NoSuchMethodError was thrown building:
The method '[]' was called on null.
Receiver: null
Tried calling: [](3)

也指出了
的错误 PostModel Post Line .. [索引]准确地说

I'm trying to create a user feed, just like that of twitter using Firebase & GetX.
In the code snippet is my function..

List<PostModel> postListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
  return PostModel(
    id: doc.id,
    text: (doc.data() as dynamic)["text"] ?? "",
    creator: (doc.data() as dynamic)["creator"] ?? "",
    timestamp: (doc.data() as dynamic)["timestamp"] ?? 0,
  );
}).toList();
}

Future<List<PostModel>> getFeed() async {
List<String> usersFollowing = await UserService() //['uid1', 'uid2']
    .getUserFollowing(FirebaseAuth.instance.currentUser!.uid);

QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("posts").where('creator', whereIn: usersFollowing)
.orderBy('timestamp', descending: true)
.get();

return postListFromSnapshot(querySnapshot);
}

What I want to do is to display the Future function getFeed(), I'm using GetX for state management. So, my problem is how can I display the result of this function using a ListView.Builder()

Here's how I used the Future builder

FutureBuilder(
  future: _.listPost,
  initialData: [PostModel(id: "2", creator: "Fm", text: "Testing", timestamp: Timestamp.now())],
  builder: (BuildContext context, AsyncSnapshot snapshot){
    if(snapshot.hasData == null){
      return Text("Data is available");
    } else{
      return ListView.builder(
        shrinkWrap: true,
        itemCount: snapshot.data.toString().length,
        itemBuilder: (context, index){
          PostModel posts = snapshot.data[index];
          return Column(
            children: [
              Text(posts.text)
            ],
          );
        },
      );
    }
  },
)

And here's the error I got

The following NoSuchMethodError was thrown building:
The method '[]' was called on null.
Receiver: null
Tried calling: [](3)

It also pointed to an error on the
PostModel post line.. the [index] to be precise

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-02-09 23:53:25

首先,使您的asyncsnapshot快照 asyncsnapshot&lt; list&lt; postmodel&gt;&gt;&gt;快照。这不是您的主要问题,但是要使正确的键入并且不必猜测dynamic,这将使事情变得容易得多。

您的问题是Hasdatabool。它是truefalse,但从不null。我想知道您是如何超越编译器的。您是否正在使用过时的版本的颤音?您应该检查一下,您的编译器是您的朋友,如果没有适当的帮助,这将是一条艰难而艰难的道路。

无论如何,您应该检查是否有数据,如果没有数据,您仍在等待:

FutureBuilder(
  future: _.listPost,
  builder: (BuildContext context, AsyncSnapshot<List<PostModel>> snapshot){
    if(!snapshot.hasData){
      return CircularProgressIndicator();
    } else {
      final postList = snapShot.requireData;

      return ListView.builder(
        shrinkWrap: true,
        itemCount: postList .length,
        itemBuilder: (context, index){
          final post = postList[index];
          return Column(
            children: [
              Text(post.text)
            ],
          );
        },
      );
    }
  },
)

First, make your AsyncSnapshot snapshot an AsyncSnapshot<List<PostModel>> snapshot. That is not your primary problem, but it will make things a lot easier to have proper typing and not have to guess around using dynamic.

Your problem is that hasData is a bool. It is either true or false, but never null. I wonder how you got that line past your compiler. Are you using an outdated version of Flutter? You should check this, your compiler is your friend, if it isn't helping you properly, this will be a hard and rocky road.

Anyway, you should check whether there is data, if there is none, you are still waiting:

FutureBuilder(
  future: _.listPost,
  builder: (BuildContext context, AsyncSnapshot<List<PostModel>> snapshot){
    if(!snapshot.hasData){
      return CircularProgressIndicator();
    } else {
      final postList = snapShot.requireData;

      return ListView.builder(
        shrinkWrap: true,
        itemCount: postList .length,
        itemBuilder: (context, index){
          final post = postList[index];
          return Column(
            children: [
              Text(post.text)
            ],
          );
        },
      );
    }
  },
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文