英文:
How to receive data from nested collection in Firebase using StreamBuilder and ListView?
问题
我想要接收下一个路径的所有音轨:
"main_page_items" 集合 -> album_id 文档 -> "tracks" 集合 -> track_id 文档
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:app/album.dart';
class SongList extends StatelessWidget {
final callback;
final album;
SongList(this.callback, this.album);
@override
Widget build(BuildContext context) {
return Container(
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection("tracks")
.where("main_page_items", isEqualTo: album)
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
final List<DocumentSnapshot> tracks = snapshot.data!.docs;
//print(tracks);
print(tracks.length);
return ListView.builder(
itemCount: tracks.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 16, 12),
child: SongItem(
tracks[index]['title'],
tracks[index]['artist'],
tracks[index]['image'],
tracks[index]['url'],
callback),
);
},
);
},
),
);
}
}
class SongItem extends StatelessWidget {
final title;
final artist;
final image;
final url;
final callback;
SongItem(this.title, this.artist, this image, this url, this.callback);
@override
Widget build(BuildContext context) {
return Column(
children: [
GestureDetector(
onTap: () {
callback(title, artist, image, url);
},
child: Row(
children: <Widget>[
Stack(
children: <Widget>[
Container(
height: 60.0,
width: 60.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.network(
image,
fit: BoxFit.cover,
),
),
),
],
),
SizedBox(width: 16.0),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
title,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18.0),
),
SizedBox(height: 8.0),
Text(
artist,
style: TextStyle(
color: Colors.white.withOpacity(0.5), fontSize: 14.0),
),
],
),
],
),
),
],
);
}
}
但如果我得到长度为 "0" 的快照值,并尝试打印文档(tracks 变量),我得到一个空的 [],但在 Firestore 数据库中我有一个元素。问题出在哪里,如何修复它?谢谢。
英文:
I want to receive all tracks from next path:
"main_page_items" Collection -> album_id Document -> "tracks" Collection -> track_id Document
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:app/album.dart';
class SongList extends StatelessWidget {
final callback;
final album;
SongList(this.callback, this.album);
@override
Widget build(BuildContext context) {
return Container(
child: StreamBuilder<QuerySnapshot>(
stream : FirebaseFirestore.instance
.collection("tracks")
.where("main_page_items", isEqualTo: album).snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
final List<DocumentSnapshot> tracks = snapshot.data!.docs ;
//print(tracks);
print(tracks.length);
return ListView.builder(
itemCount: tracks.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 16, 12),
child: SongItem(
tracks[index]['title'],
tracks[index]['artist'],
tracks[index]['image'],
tracks[index]['url'],
callback),
);
},
);
},
),
);
}
}
class SongItem extends StatelessWidget {
final title;
final artist;
final image;
final url;
final callback;
SongItem(this.title, this.artist, this.image, this.url, this.callback);
@override
Widget build(BuildContext context) {
return Column(
children: [
GestureDetector(
onTap: () {
callback(title, artist, image, url);
},
child: Row(
children: <Widget>[
Stack(
children: <Widget>[
Container(
height: 60.0,
width: 60.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.network(
image,
fit: BoxFit.cover,
),
),
),
],
),
SizedBox(width: 16.0),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
title,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18.0),
),
SizedBox(height: 8.0),
Text(
artist,
style: TextStyle(
color: Colors.white.withOpacity(0.5), fontSize: 14.0),
),
],
),
],
),
),
],
);
}
}
But if i've got a "0" value of length of snapshot, and if i try to print the docs (tracks variable), i've got an empty [], but i have one element in the Firestore DB.
What is the problem and how to fix it? Thank you.
答案1
得分: 1
id doesn't seem that the database links that you said it wat actually you used in the code :
FirebaseFirestore.instance
.collection("tracks")
.where("main_page_items", isEqualTo: album).snapshots(),
by using this, you're likely refer to tracks as the most parent collection in your database, but from what you provided, that tracks is a sub-collection and should be accessed like this:
FirebaseFirestore.instance
.collection("main_page_items")
.document("album_id")
.collection("tracks")
.snapshots(),
英文:
id doesn't seem that the database links that you said it wat actually you used in the code :
FirebaseFirestore.instance
.collection("tracks")
.where("main_page_items", isEqualTo: album).snapshots(),
by using this, you're likely refer to tracks as a the most parent collection in your database, but from what you provided, that tracks is a sub-collection and should be accessed like this:
FirebaseFirestore.instance
.collection("main_page_items")
.document("album_id")
.collection("tracks")
.snapshots(),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论