在列表中显示来自firebase的数据,如果我按下其中一个项目,它会将我带到完整的数据页面

6qqygrtg  于 2023-01-02  发布在  其他
关注(0)|答案(2)|浏览(110)

我在我的毕业设计中使用flutter web,并将数据保存在firebase中,因此我使用以下代码显示数据

StreamBuilder<QuerySnapshot> (
    stream: FirebaseFirestore.instance.collection('Guests').snapshots(),
    builder: (context, snapshots) {
      return (snapshots.connectionState == ConnectionState.waiting)
          ? Center(
        child: CircularProgressIndicator(),)
          : ListView.builder(
          itemCount: snapshots.data!.docs.length,
          itemBuilder: (context, index){
            var data = snapshots.data!.docs[index].data() as Map<String,dynamic>;

            return ListTile(
              title: Text(
                data['Drug Name'],
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    color: Colors.black54,
                    fontSize: 20,
                    fontWeight: FontWeight.bold),
              ),
              subtitle: Text(
                data['Drug Serial Number'],
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    color: Colors.black54,
                    fontSize: 16,
                    fontWeight: FontWeight.bold),
              ),
            );

the List of the items的输出,所以我想当按下其中一个应用程序带我到另一个页面,显示该项目的全部数据(其字段和子集合及其字段).
如果有人知道怎么做,我会很感激
我在试着找一段代码来完成我的要求

t5fffqht

t5fffqht1#

创建一个新页面,您希望在其中显示详细信息。我们将其命名为DetailsPage:

class DetailsPage extends StatelessWidget {
  const DetailsPage({Key? key,required this.details,required this.docId}) : super(key: key);
  final Map<String,dynamic> details; // <== this field holds the details.
  final String docId;
  final String subCollection = ''; // <== specify name of your sub collection

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: FirebaseFirestore.instance
            .document(docId)
            .collection(subCollection)
            .get(),
        builder: (context, snapshot){
          if (snapshot.connectionState == ConnectionState.done) {
            // If we got an error
            if (snapshot.hasError) {
              return Center(
                child: Text(
                  '${snapshot.error} occurred',
                  style: TextStyle(fontSize: 18),
                ),
              );

              // if we got our data
            } else if (snapshot.hasData) {
              // Extracting data from snapshot object
              final Map<String,dynamic>? data = snapshot.data as Map<String,dynamic>;
              //return your widget for details page here.
              return Container();
            }
          }

          // Displaying LoadingSpinner to indicate waiting state
          return Center(
            child: CircularProgressIndicator(),
          );
        },
      ),
    );
  }
}

并添加一个方法,以便在单击上一页中的特定列表项时导航到此页。ListTile提供了一个名为onTap的方法,可用作:

StreamBuilder<QuerySnapshot> (
        stream: FirebaseFirestore.instance.collection('Guests').snapshots(),
    builder: (context, snapshots) {
    return (snapshots.connectionState == ConnectionState.waiting)
    ? Center(
    child: CircularProgressIndicator(),)
        : ListView.builder(
    itemCount: snapshots.data!.docs.length,
    itemBuilder: (context, index){
    var data = snapshots.data!.docs[index].data() as Map<String,dynamic>;

    return ListTile(
    onTap: (){
    Navigator.of(context).push(MaterialPageRoute(builder: (context)=> DetailsPage(
      details: data,
      docId: snapshots.data!.docs[index].id,
    )))}
    },
    title: Text(
    data['Drug Name'],
    maxLines: 1,
    overflow: TextOverflow.ellipsis,
    style: TextStyle(
    color: Colors.black54,
    fontSize: 20,
    fontWeight: FontWeight.bold),
    ),
    subtitle: Text(
    data['Drug Serial Number'],
    maxLines: 1,
    overflow: TextOverflow.ellipsis,
    style: TextStyle(
    color: Colors.black54,
    fontSize: 16,
    fontWeight: FontWeight.bold),
    ),
    );
zzlelutf

zzlelutf2#

您可以使用ListTile.onTap()Navigator API移动显示整个数据的页面。
detailed 页面中,您可以使用DocumentReference.collection(String collectionPath)cloud_firebase package中获取子集合,使用Reference.getData()firebase_storage package中获取文件(如果存储在firebase存储中
为了不获取相同的数据,您可以尝试Proxy模式。尝试阅读this。它可能适合您的情况。

相关问题