【问题标题】:single document in same collection Firestore Flutter同一集合中的单个文档 Firestore Flutter
【发布时间】:2020-03-16 12:48:05
【问题描述】:

我构建了一个测验应用程序并使用 firestore 存储数据,我需要一个代码:当用户选择答案 1 时,他转到页面 A,但当他选择答案 2 时,他转到页面 B ... 等等

这就是我的位置:当我点击它时,所有答案都会转到同一页面,我希望每个答案都有自己的页面

这是我的代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class question14 extends StatefulWidget {
  @override
  _question14State createState() => _question14State();
}

class _question14State extends State<question14> {
  int selectedIndex 

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: StreamBuilder(
            stream: Firestore.instance.collection('numberzz').snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) return const Text('Loading ...');
              return ListView.builder(

                scrollDirection: Axis.horizontal,
                padding: EdgeInsets.fromLTRB(100.0, 0.0, 0.0, 0.0),
                itemExtent: 200.0,
                itemCount: snapshot.data.documents.length,
                itemBuilder: (BuildContext context, int index) {
                  final DocumentSnapshot document =
                      snapshot.data.documents[index];

                  return Container(
                    padding: EdgeInsets.fromLTRB(0.0, 300.0, 0.0, 450.0),
                    child: ListTile(
                      contentPadding: selectedIndex == index
                          ? EdgeInsets.all(0.0)
                          : EdgeInsets.all(25.0),
                      title: Image.network(
                        document['number'],
                      ),
                      selected: selectedIndex == index,
                      onTap: () {
                        Firestore.instance.runTransaction((transaction) async {
                          DocumentSnapshot freshSnap =
                              await transaction.get(document.reference);
                          await transaction.update(freshSnap.reference, {
                            'vote': freshSnap['vote'] + 1,
                          });
                        });
                        Navigator.push(
                                context, MaterialPageRoute(
                                builder: (context) => new page()));

                        setState(() {
                          selectedIndex = index;
                        });
                      },
                    ),
                  );
                },
              );
            }));
  }
}

感谢您的帮助!

【问题讨论】:

  • 好的,那么到目前为止你的代码做了什么?显示器有问题吗?代码符合家庭作业要求?
  • 现在我的实际代码让我选择一个项目并重定向到同一页面,我希望每个项目都有他自己的页面..当用户点击数字“1”时,他会转到页面“A” ,当他点击数字“2”时,他会转到另一个页面“B”,当用户点击数字“3”时,他会转到页面“C”。

标签: flutter dart google-cloud-firestore


【解决方案1】:

您可以在导航之前检查选择了哪个索引。一种条件导航。

switch(selectedIndex){
 case 0:
   Navigator.of(context).push(.....(Page A));
   break;
 case 1:
  .......
  break; 

我想你明白了。 但是这段代码应该在

之后
setState(() { selectedIndex = index };

【讨论】:

  • 我编辑了我的第一篇文章,通过更改 gif 来解释我需要的更多内容,谢谢您的关注!
猜你喜欢
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 2020-09-12
  • 2021-12-16
  • 2021-08-26
  • 2020-12-02
相关资源
最近更新 更多