如何使用StreamBuilder和ListView从Firebase中的嵌套集合接收数据?

huangapple go评论56阅读模式
英文:

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 &#39;package:flutter/material.dart&#39;;
import &#39;package:cloud_firestore/cloud_firestore.dart&#39;;
import &#39;package:app/album.dart&#39;;
class SongList extends StatelessWidget {
final callback;
final album;
SongList(this.callback, this.album);
@override
Widget build(BuildContext context) {
return Container(
child: StreamBuilder&lt;QuerySnapshot&gt;(
stream : FirebaseFirestore.instance
.collection(&quot;tracks&quot;)
.where(&quot;main_page_items&quot;, isEqualTo: album).snapshots(),
builder: (BuildContext context, AsyncSnapshot&lt;QuerySnapshot&gt; snapshot) {
final List&lt;DocumentSnapshot&gt; 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][&#39;title&#39;],
tracks[index][&#39;artist&#39;],
tracks[index][&#39;image&#39;],
tracks[index][&#39;url&#39;],
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: &lt;Widget&gt;[
Stack(
children: &lt;Widget&gt;[
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: &lt;Widget&gt;[
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(&quot;tracks&quot;)
.where(&quot;main_page_items&quot;, 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(&quot;main_page_items&quot;)
.document(&quot;album_id&quot;)
.collection(&quot;tracks&quot;)
.snapshots(),

huangapple
  • 本文由 发表于 2023年2月23日 20:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定