Firestore查询中的Flutter传递文档ID

Bnd10706

我试图弄清楚该怎么做。

如果我通过硬编码的文档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"],
                          ),
                        );
                      }),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

类别小部件是我遇到问题的地方。

评论列出了我遇到的问题。

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),)
              ],
            ),
          ),
        ],
      ),
    );
  }
}
艾克·毛维拉

我已经意识到该代码在注释中看起来很可疑,所以让我回答。从GitHub问题12可以看出,当前正在不支持您尝试执行的操作

将您的代码更改为

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

  List<DocumentSnapshot> snapshot;

  Query collectionReference;

  void initState() {
    collectionReference = Firestore.instance.collection("mainCategories").document(widget.category).collection("subCategories").orderBy("title");
    subscription = collectionReference.snapshots().listen((datasnapshot) {
      setState(() {
        snapshot = datasnapshot.documents;
      });
    });
    super.initState();
  }

...

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Cloud Firestore查询以获取文档ID,Flutter

来自分类Dev

如何从Flutter中使用Firestore中的唯一文档ID进行查询?

来自分类Dev

使用Flutter从Firebase Firestore查询中获取单个文档

来自分类常见问题

在Firestore数据库中查询文档ID

来自分类Dev

在Flutter Cloud Firestore中设置自定义文档ID

来自分类Dev

将用户uid传递到Flutter中的Firestore流查询

来自分类Dev

Firestore / Flutter:查询文档的子集合

来自分类Dev

在多个文档ID中的Firestore

来自分类Dev

如何从Cloud Firestore中获取user.uid等于flutter中的文档ID的数据?

来自分类Dev

在子集合Firestore中查询文档

来自分类Dev

Firestore从集合中查询多个文档

来自分类Dev

从firestore的文档查询集合中创建列表

来自分类Dev

在 firestore 中返回带有查询的文档

来自分类Dev

在flutter中从cloud_firestore删除文档

来自分类Dev

如何使用Flutter / Dart根据选择将单个Firestore文档ID传递给另一个类?

来自分类Dev

如何使用flutter从Firestore中检索具有其ID的文档

来自分类Dev

获取Firestore中已删除文档的ID

来自分类Dev

Flutter Firestore:集合中的特定文档 (Firestore.collection.where)

来自分类Dev

如何使用文档 ID cloud firestore 为查询创建索引

来自分类Dev

Firestore查询单个文档

来自分类Dev

Firestore 查询了很多文档

来自分类Dev

集合文档中具有随机ID的Firestore嵌套文档

来自分类Dev

在 Firestore 中创建文档时无法指定文档 ID

来自分类Dev

Firestore集合查询在Flutter中作为流

来自分类Dev

Flutter Fire中的Firestore动态查询

来自分类Dev

如何从AnimalDB查询中检索文档ID

来自分类Dev

查询CouchDb中特定文档ID的更改

来自分类Dev

我可以查询Firestore中嵌套文档中的字段吗?

来自分类Dev

如何使用Flutter在Firestore中的uid文档中创建集合?

Related 相关文章

  1. 1

    Cloud Firestore查询以获取文档ID,Flutter

  2. 2

    如何从Flutter中使用Firestore中的唯一文档ID进行查询?

  3. 3

    使用Flutter从Firebase Firestore查询中获取单个文档

  4. 4

    在Firestore数据库中查询文档ID

  5. 5

    在Flutter Cloud Firestore中设置自定义文档ID

  6. 6

    将用户uid传递到Flutter中的Firestore流查询

  7. 7

    Firestore / Flutter:查询文档的子集合

  8. 8

    在多个文档ID中的Firestore

  9. 9

    如何从Cloud Firestore中获取user.uid等于flutter中的文档ID的数据?

  10. 10

    在子集合Firestore中查询文档

  11. 11

    Firestore从集合中查询多个文档

  12. 12

    从firestore的文档查询集合中创建列表

  13. 13

    在 firestore 中返回带有查询的文档

  14. 14

    在flutter中从cloud_firestore删除文档

  15. 15

    如何使用Flutter / Dart根据选择将单个Firestore文档ID传递给另一个类?

  16. 16

    如何使用flutter从Firestore中检索具有其ID的文档

  17. 17

    获取Firestore中已删除文档的ID

  18. 18

    Flutter Firestore:集合中的特定文档 (Firestore.collection.where)

  19. 19

    如何使用文档 ID cloud firestore 为查询创建索引

  20. 20

    Firestore查询单个文档

  21. 21

    Firestore 查询了很多文档

  22. 22

    集合文档中具有随机ID的Firestore嵌套文档

  23. 23

    在 Firestore 中创建文档时无法指定文档 ID

  24. 24

    Firestore集合查询在Flutter中作为流

  25. 25

    Flutter Fire中的Firestore动态查询

  26. 26

    如何从AnimalDB查询中检索文档ID

  27. 27

    查询CouchDb中特定文档ID的更改

  28. 28

    我可以查询Firestore中嵌套文档中的字段吗?

  29. 29

    如何使用Flutter在Firestore中的uid文档中创建集合?

热门标签

归档