Home > database >  how to limit the data from Firestore
how to limit the data from Firestore

Time:07-08

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(),),),);}, ); }}

CodePudding user response:

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.

CodePudding user response:

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();
  • Related