Firebase 在Firestore查询中传递文档ID时出错

Firebase 在Firestore查询中传递文档ID时出错,firebase,firebase-realtime-database,flutter,dart,google-cloud-firestore,Firebase,Firebase Realtime Database,Flutter,Dart,Google Cloud Firestore,我正在想办法做这件事 如果我通过硬编码的文档ID,它就会工作。但是,由于我有多个页面,我正在试图找出如何传递它,然后查询它 我的主页有以下内容 import 'package:flutter/material.dart'; import 'package:onlytag2/pages/category.dart'; import 'package:onlytag2/widget/maincard.dart'; import 'package:cloud_firestore/cloud_fires

我正在想办法做这件事

如果我通过硬编码的文档ID,它就会工作。但是,由于我有多个页面,我正在试图找出如何传递它,然后查询它

我的主页有以下内容

import 'package:flutter/material.dart';
import 'package:onlytag2/pages/category.dart';
import 'package:onlytag2/widget/maincard.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  StreamSubscription<QuerySnapshot> subscription;

  List<DocumentSnapshot> snapshot;

  Query collectionReference = Firestore.instance.collection("mainCategories").orderBy("title");

  void initState() {
    subscription = collectionReference.snapshots().listen((datasnapshot) {
      setState(() {
        snapshot = datasnapshot.documents;
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    subscription.cancel(); //Streams must be closed when not needed
    super.dispose();
  }

  passData(DocumentSnapshot snap, String cat, String title) {
    print(cat);
    Navigator.of(context).push(
        MaterialPageRoute(builder: (context) => Category(snapshot: snap, category: cat, title: title,)));
  }

  @override
  Widget build(BuildContext context) {
    if (snapshot == null) return Center(
      child: Container(
        color: Colors.black,
        alignment: AlignmentDirectional(0.0, 0.0),
        child: Container(
          color: Colors.black,
          constraints: BoxConstraints(
              maxHeight: 300.0,
              maxWidth: 200.0,
              minWidth: 150.0,
              minHeight: 150.0
          ),
          child: CircularProgressIndicator(),
        ),
      ),
    );
    return Scaffold(
      backgroundColor: Color(0xff0E0E0F),
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.black,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "#",
              style: TextStyle(
                  fontSize: 25, color: Color(0xffff9900), fontFamily: 'Dokyo'),
            ),
            Text(
              "onlytags",
              style: TextStyle(color: Colors.white, fontFamily: 'Dokyo'),
            )
          ],
        ),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Column(
              children: <Widget>[
                Expanded(
                  child: ListView.builder(
                      itemCount: snapshot.length,
                      itemBuilder: (context, index) {
                        return InkWell(
                          onTap: () => passData(snapshot[index], snapshot[index].documentID.toString(), snapshot[index].data["title"] ),
                          child: MainCard(
                            title: snapshot[index].data["title"],
                            subtitle: snapshot[index].data["subTitle"],
                            image: snapshot[index].data["image"],
                          ),
                        );
                      }),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“package:onlytag2/pages/category.dart”;
导入“package:onlytag2/widget/maincard.dart”;
导入“包:cloud_firestore/cloud_firestore.dart”;
导入“dart:async”;
类Home扩展了StatefulWidget{
@凌驾
_HomeState createState()=>\u HomeState();
}
类(HomeState扩展状态){
流媒体订阅;
列表快照;
Query collectionReference=Firestore.instance.collection(“主要类别”).orderBy(“标题”);
void initState(){
订阅=collectionReference.snapshots().listen((datasnapshot){
设置状态(){
快照=datasnapshot.documents;
});
});
super.initState();
}
@凌驾
无效处置(){
subscription.cancel();//不需要时必须关闭流
super.dispose();
}
passData(文档快照快照、字符串类别、字符串标题){
印刷品(cat);
导航器.of(上下文).push(
MaterialPage路由(生成器:(上下文)=>类别(快照:快照,类别:cat,标题:标题,);
}
@凌驾
小部件构建(构建上下文){
如果(快照==null)返回中心(
子:容器(
颜色:颜色,黑色,
对齐:对齐方向(0.0,0.0),
子:容器(
颜色:颜色,黑色,
约束:BoxConstraints(
最大高度:300.0,
最大宽度:200.0,
最小宽度:150.0,
最小高度:150.0
),
子对象:CircularProgressIndicator(),
),
),
);
返回脚手架(
背景颜色:颜色(0xFF0E0F),
appBar:appBar(
标题:对,
背景颜色:Colors.black,
标题:世界其他地区(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
"#",
样式:TextStyle(
fontSize:25,颜色:color(0xffff9900),fontFamily:'Dokyo',
),
正文(
“仅限标签”,
样式:TextStyle(颜色:Colors.white,fontFamily:“Dokyo”),
)
],
),
),
正文:专栏(
儿童:[
扩大(
子:列(
儿童:[
扩大(
子项:ListView.builder(
itemCount:snapshot.length,
itemBuilder:(上下文,索引){
回墨槽(
onTap:()=>passData(快照[索引],快照[索引].documentID.toString(),快照[索引].data[“标题]),
孩子:主卡(
标题:快照[索引]。数据[“标题”],
副标题:快照[索引]。数据[“副标题”],
图像:快照[索引]。数据[“图像”],
),
);
}),
),
],
),
),
],
),
);
}
}
类别小部件就是我遇到问题的地方

import 'package:flutter/material.dart';
import 'package:onlytag2/widget/sub.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';

class Category extends StatefulWidget {
  DocumentSnapshot snapshot;
  final String category;
  final String title;

  Category({this.snapshot, this.category, this.title});

  @override
  _CategoryState createState() => _CategoryState();
}

class _CategoryState extends State<Category>{
  StreamSubscription<QuerySnapshot> subscription;

  List<DocumentSnapshot> snapshot;

  //How do I get the category passed properly? I know it is the Document ID, but since it changes based on what button is pressed before,
  //I cant figure out how to pass it..
  Query collectionReference = Firestore.instance.collection("mainCategories").document(widget.category).collection("subCategories").orderBy("title");

  void initState() {
    subscription = collectionReference.snapshots().listen((datasnapshot) {
      setState(() {
        snapshot = datasnapshot.documents;
      });
    });
    super.initState();
  }


  @override
  void dispose() {
    subscription.cancel(); //Streams must be closed when not needed
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (snapshot == null) return Center(
      child: Container(
        color: Colors.black,
        alignment: AlignmentDirectional(0.0, 0.0),
        child: Container(
          color: Colors.black,
          constraints: BoxConstraints(
              maxHeight: 300.0,
              maxWidth: 200.0,
              minWidth: 150.0,
              minHeight: 150.0
          ),
          child: CircularProgressIndicator(),
        ),
      ),
    );
    return Scaffold(
      backgroundColor: Color(0xff0E0E0F),
      appBar: AppBar(
        iconTheme: IconThemeData(
          color: Color(0xffff9900),
        ),
        centerTitle: true,
        backgroundColor: Colors.black,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              "#", style: TextStyle(fontSize: 25, color: Color(0xffff9900), fontFamily: 'Dokyo'),
            ),
            Text(widget.title.toLowerCase(), style: TextStyle(color: Colors.white, fontFamily: 'Dokyo'),)
          ],
        ),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Column(
              children: <Widget>[
                Expanded(
                  child: ListView.builder(
                      itemCount: snapshot.length,
                      itemBuilder: (context, index) {
                        return Sub(
                          title: snapshot[index].data["title"],
                          subtitle: snapshot[index].data["subTitle"],
                          image: snapshot[index].data["image"],
                        );
                      }),
                ),
                Padding(padding: EdgeInsets.fromLTRB(0, 0, 0, 15),)
              ],
            ),
          ),
        ],
      ),
    );
  }
}
注释列出了我的问题所在

import 'package:flutter/material.dart';
import 'package:onlytag2/widget/sub.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';

class Category extends StatefulWidget {
  DocumentSnapshot snapshot;
  final String category;
  final String title;

  Category({this.snapshot, this.category, this.title});

  @override
  _CategoryState createState() => _CategoryState();
}

class _CategoryState extends State<Category>{
  StreamSubscription<QuerySnapshot> subscription;

  List<DocumentSnapshot> snapshot;

  //How do I get the category passed properly? I know it is the Document ID, but since it changes based on what button is pressed before,
  //I cant figure out how to pass it..
  Query collectionReference = Firestore.instance.collection("mainCategories").document(widget.category).collection("subCategories").orderBy("title");

  void initState() {
    subscription = collectionReference.snapshots().listen((datasnapshot) {
      setState(() {
        snapshot = datasnapshot.documents;
      });
    });
    super.initState();
  }


  @override
  void dispose() {
    subscription.cancel(); //Streams must be closed when not needed
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (snapshot == null) return Center(
      child: Container(
        color: Colors.black,
        alignment: AlignmentDirectional(0.0, 0.0),
        child: Container(
          color: Colors.black,
          constraints: BoxConstraints(
              maxHeight: 300.0,
              maxWidth: 200.0,
              minWidth: 150.0,
              minHeight: 150.0
          ),
          child: CircularProgressIndicator(),
        ),
      ),
    );
    return Scaffold(
      backgroundColor: Color(0xff0E0E0F),
      appBar: AppBar(
        iconTheme: IconThemeData(
          color: Color(0xffff9900),
        ),
        centerTitle: true,
        backgroundColor: Colors.black,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              "#", style: TextStyle(fontSize: 25, color: Color(0xffff9900), fontFamily: 'Dokyo'),
            ),
            Text(widget.title.toLowerCase(), style: TextStyle(color: Colors.white, fontFamily: 'Dokyo'),)
          ],
        ),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Column(
              children: <Widget>[
                Expanded(
                  child: ListView.builder(
                      itemCount: snapshot.length,
                      itemBuilder: (context, index) {
                        return Sub(
                          title: snapshot[index].data["title"],
                          subtitle: snapshot[index].data["subTitle"],
                          image: snapshot[index].data["image"],
                        );
                      }),
                ),
                Padding(padding: EdgeInsets.fromLTRB(0, 0, 0, 15),)
              ],
            ),
          ),
        ],
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“package:onlytag2/widget/sub.dart”;
导入“包:cloud_firestore/cloud_firestore.dart”;
导入“dart:async”;
类类别扩展了StatefulWidget{
文档快照;
最终字符串类别;
最后的字符串标题;
类别({this.snapshot,this.Category,this.title});
@凌驾
_CategoryState createState()=>U CategoryState();
}
类_CategoryState扩展状态{
流媒体订阅;
列表快照;
//如何正确传递类别?我知道它是文档ID,但由于它根据之前按下的按钮而变化,
//我想不出怎么通过考试。。
Query collectionReference=Firestore.instance.collection(“maincegories”).document(widget.category).collection(“子类别”).orderBy(“标题”);
void initState(){
订阅=collectionReference.snapshots().listen((datasnapshot){
设置状态(){
快照=datasnapshot.documents;
});
});
super.initState();
}
@凌驾
无效处置(){
subscription.cancel();//不需要时必须关闭流
super.dispose();
}
@凌驾
小部件构建(构建上下文){
如果(快照==null)返回中心(
子:容器(
颜色:颜色,黑色,
对齐:对齐方向(0.0,0.0),
子:容器(
颜色:颜色,黑色,
约束:BoxConstraints(
最大高度:300.0,
最大宽度:200.0,
最小宽度:150.0,
最小高度:150.0
),
子对象:CircularProgressIndicator(),
),
),
);
返回脚手架(
背景颜色:颜色(0xFF0E0F),
appBar:appBar(
iconTheme:IconThemeData(
颜色:颜色(0xffff9900),
),
标题:对,
背景颜色:Colors.black,
标题:世界其他地区(
mainAxisAlignment:mainAxisAlignment.center,
mainAxisSize:mainAxisSize.min,
儿童:[
正文(
“#”,样式:TextStyle(fontSize:25,颜色:color(0xffff9900),fontFamily:'Dokyo'),
),
文本(widget.title.toLowerCase(),样式:TextStyle(颜色:Colors.white,fontFamily:'Dokyo'),)
],
),
),
正文:专栏(
儿童:[
扩大(
子:列(
儿童:[
扩大(
子项:ListView.builder(
itemCount:snapshot.length,
itemBuilder:(上下文,索引){