如何限制来自Firestore的数据

发布于 2025-02-13 21:49:47 字数 3463 浏览 0 评论 0原文

我想显示来自Firebase Firestore的最新数据。我只需要最新的3个文档。我确实尝试了ListView中的ItemCount,但它不起作用,并且也崩溃了。任何帮助将不胜感激。预先感谢您,如果架子架的架子不像我的代码那样长,则不接受我的问题。

class NewInHouses extends StatefulWidget {
  const NewInHouses({Key? key}) : super(key: key);

  @override
  State<NewInHouses> createState() => _NewInHousesState();
}
class _NewInHousesState extends State<NewInHouses> {
  final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('houseRent').snapshots();
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _usersStream,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return const Text('Something went wrong');
        }
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Text("Loading");
        }
        return Padding(
          padding: const EdgeInsets.all(5.0),
          child: Container(
            height: 160,
            width: 2000,
            color: Colors.white.withOpacity(0),
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: snapshot.data!.docs.map((DocumentSnapshot document) {
                Map<String, dynamic> data =
                    document.data()! as Map<String, dynamic>;
                return Padding(
                  padding: const EdgeInsets.fromLTRB(0, 2.5, 10, 2.5),
                  child: Container(
                    height: 150,
                    width: 300,
                    decoration: const BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.grey,
                            spreadRadius: 1,
                            blurRadius: 1,
                          )]),
                    child: Row(
                      children: [
                        Container(
                          width: 150,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: const BorderRadius.only(
                              topLeft: Radius.circular(5),
                              bottomLeft: Radius.circular(5),),
                            image: DecorationImage(
                              image: CachedNetworkImageProvider(
                                data["1stpic"],),
                            ),),),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            children: [
                              Text(
                                data["rooms"] +
                                    " Beds  " +
                                    data["baths"] +
                                    " Baths",
                                style: TextStyle(
                                    fontFamily: "eng",
                                    fontWeight: FontWeight.bold,
                                    color: Colors.blue.shade900,
                                    fontSize: 12),
                              ),],),),],),),);}).toList(),),),);}, ); }}

I want to show the latest data from from firebase firestore. I want only the latest 3 documents. I did try the ItemCount in ListView but it doesn't work and it crashes the app too. any help will be highly appreciated. thanks in advance, sorry about the braces stackOverflow is not accepting my question if it is not as long as my code.

class NewInHouses extends StatefulWidget {
  const NewInHouses({Key? key}) : super(key: key);

  @override
  State<NewInHouses> createState() => _NewInHousesState();
}
class _NewInHousesState extends State<NewInHouses> {
  final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('houseRent').snapshots();
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _usersStream,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return const Text('Something went wrong');
        }
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Text("Loading");
        }
        return Padding(
          padding: const EdgeInsets.all(5.0),
          child: Container(
            height: 160,
            width: 2000,
            color: Colors.white.withOpacity(0),
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: snapshot.data!.docs.map((DocumentSnapshot document) {
                Map<String, dynamic> data =
                    document.data()! as Map<String, dynamic>;
                return Padding(
                  padding: const EdgeInsets.fromLTRB(0, 2.5, 10, 2.5),
                  child: Container(
                    height: 150,
                    width: 300,
                    decoration: const BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.grey,
                            spreadRadius: 1,
                            blurRadius: 1,
                          )]),
                    child: Row(
                      children: [
                        Container(
                          width: 150,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: const BorderRadius.only(
                              topLeft: Radius.circular(5),
                              bottomLeft: Radius.circular(5),),
                            image: DecorationImage(
                              image: CachedNetworkImageProvider(
                                data["1stpic"],),
                            ),),),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            children: [
                              Text(
                                data["rooms"] +
                                    " Beds  " +
                                    data["baths"] +
                                    " Baths",
                                style: TextStyle(
                                    fontFamily: "eng",
                                    fontWeight: FontWeight.bold,
                                    color: Colors.blue.shade900,
                                    fontSize: 12),
                              ),],),),],),),);}).toList(),),),);}, ); }}

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

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

发布评论

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

评论(2

揽月 2025-02-20 21:49:48

final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('houseRent').snapshots();

final Stream<QuerySnapshot> _usersStream =
FirebaseFirestore.instance.collection('houseRent').orderBy('your-field-name', descending: true).limit(3).snapshots();

replace this

final Stream<QuerySnapshot> _usersStream =
      FirebaseFirestore.instance.collection('houseRent').snapshots();

with

final Stream<QuerySnapshot> _usersStream =
FirebaseFirestore.instance.collection('houseRent').orderBy('your-field-name', descending: true).limit(3).snapshots();
完美的未来在梦里 2025-02-20 21:49:48

您可以在.collection('Houserent')之后添加。 /code>您可以下降它,以便最后一个位于顶部。

You can add a .limit(3) right after the .collection('houseRent') with an .orderBy('insert-field-here') an you can descend it so the last ones come on top.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文