【问题标题】:How to use Variables Cloud Firestore in Flutter (Dart) while doing query查询时如何在 Flutter (Dart) 中使用变量 Cloud Firestore
【发布时间】:2022-01-23 12:17:37
【问题描述】:

这是我的工作代码:

final Stream<QuerySnapshot> products = FirebaseFirestore.instance
  .collection('products')
  .doc('men')
  .collection('tshirts')
  .snapshots();

这是我想要但不起作用的代码:

String val = 'tshirts';
final Stream<QuerySnapshot> products = FirebaseFirestore.instance
      .collection('products')
      .doc('men')
      .collection(val)
      .snapshots();

错误: 无法在初始化程序中访问实例成员“val”。 尝试用不同的表达式替换对实例成员的引用

我在构建小部件中使用的波纹管代码显示数据库查询结果和工作正常:

GridView.count(
          physics: const BouncingScrollPhysics(),
          crossAxisCount: 2,
          childAspectRatio: 0.5,
          mainAxisSpacing: 30,
          crossAxisSpacing: 10,
          children: snapshot.data!.docs.map((DocumentSnapshot document) {
            Map<String, dynamic> data =
                document.data()! as Map<String, dynamic>;

            return Visibility(
              child: GestureDetector(
                  onTap: () {
                    Navigator.push(
                        context,
                        PageTransition(
                          type: PageTransitionType.bottomToTop,
                          child: const ProductView(productId: '3453245'),
                        ));
                  },
                  child: Stack(
                    children: [
                      Container(
                        decoration: BoxDecoration(
                            image: DecorationImage(
                                image: NetworkImage('${data['img']}'),
                                fit: BoxFit.cover)),
                      ),
                      Align(
                        alignment: Alignment.bottomLeft,
                        child: SizedBox(
                          height: 70,
                          width: double.infinity,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                margin: const EdgeInsets.only(
                                    right: 18, top: 15, left: 5),
                                child: Text(
                                  data['name'].toUpperCase(),
                                  textAlign: TextAlign.left,
                                  style: GoogleFonts.oswald(
                                    color: Colors.black,
                                    fontSize: 15,
                                  ),
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 1,
                                  softWrap: false,
                                ),
                              ),
                              Container(
                                margin: const EdgeInsets.only(left: 5),
                                child: Text(
                                  '\u{20B9} ${data['price']}',
                                  textAlign: TextAlign.left,
                                  style: const TextStyle(
                                    color: Colors.black,
                                    fontSize: 14,
                                  ),
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 1,
                                ),
                              )
                            ],
                          ),
                        ).frosted(
                          blur: 7,
                        ),
                      ),
                    ],
                  )),
              visible: true,
            );
          }).toList(),
        )

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您甚至在初始化所使用的类之前就尝试使用val。 您只能在方法或构造函数中访问它,例如:

    @override
    void initState() {
        super.initState();
        final Stream<QuerySnapshot> products = FirebaseFirestore.instance.collection('products').doc('men').collection(val).snapshots();
    }
    

    【讨论】:

    • 那如何在initState之外使用变量products??
    • @HimanshuBohemia String val = 'tshirts'; 这个你可以将它声明为一个实例变量,但你不能使用val 除非类被初始化。如果你想使用products 变量只是使用 Stream&lt;QuerySnapshot&gt;? 将其作为最初引用 null 的实例变量
    猜你喜欢
    • 2020-10-22
    • 2020-10-22
    • 2019-01-04
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    相关资源
    最近更新 更多