如何限制来自Firestore的数据
我想显示来自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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用
replace this
with
您可以在
.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.