简体   繁体   English

Flutter Firebase 在同一小部件中返回 Querysnapshot 和 DocumentSnapshot

[英]Flutter Firebase return a Querysnapshot and DocumentSnapshot in the same widget

I've spent weeks trying to sort this issue and can't seem to sort it.我花了数周时间试图解决这个问题,但似乎无法解决。

I have a database with two collections userTable and userMoods我有一个包含两个 collections userTable 和 userMoods 的数据库

I have a future builder which is returning the name, however I am querying the userMood table to return the last created document.我有一个返回名称的未来生成器,但是我正在查询 userMood 表以返回最后创建的文档。

I cannot seem to find a way to get this data back out.我似乎无法找到恢复这些数据的方法。

Picture of data I am trying to retrieve:我要检索的数据图片: 在此处输入图像描述

Code is as follows:代码如下:

class CorrectMood extends StatefulWidget {
  const CorrectMood({Key? key}) : super(key: key);

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

class _CorrectMoodState extends State<CorrectMood> {
  Future<DocumentSnapshot<Map<String, dynamic>>>? _fetchedData;

  @override
  void initState() {
    super.initState();
    _fetchedData = getData();
  }


  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _fetchedData,
      builder: (BuildContext context,
          AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) {
        if (snapshot.hasData) {
          return Scaffold(
            appBar: AppBar(
              title: const Text('Display the Picture'),
              backgroundColor: kPrimaryColor,
            ),
            // The image is stored as a file on the device. Use the `Image.file`
            // constructor with the given path to display the image.
            body: Center(
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.fromLTRB(8.0, 20.0, 8.0, 8.0),
                    child: Column(
                      children: [
                        Center(
                          child:
                          Text(
                            "${snapshot.data!.data()!["firstName"]} \n\n "
                                "We have predicted your mood as:\n\n "
                            //"${DatabaseService.getMood()}\n\n"
                                "Please select a reason associated to your mood",
                            style: const TextStyle(
                                color: Colors.black, fontSize: 15),
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        }else {
          return CircularProgressIndicator();
        }
      },
    );
  }


}

Future<DocumentSnapshot<Map<String, dynamic>>> getData() async {
  var currentUser = FirebaseAuth.instance.currentUser;
  return await FirebaseFirestore.instance
      .collection('USER_TABLE')
      .doc(currentUser!.uid)
      .get();
}

Future<QuerySnapshot<Map<String, dynamic>>> getMood()  async {
    var currentUser = FirebaseAuth.instance.currentUser;


    return await FirebaseFirestore.instance
        .collection('userMood')
        .where('userId' == currentUser!.uid)
        .orderBy('createdAt', descending: true)
        .limit(1)
        .get();

Any help is greatly appreciated!任何帮助是极大的赞赏!

you can use getData() directly to your FutureBuilder.您可以直接对您的 FutureBuilder 使用 getData()。 by the way I cannot where you are calling getMood() function.顺便说一句,我不能在哪里调用 getMood() function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 文档快照 map 到 Json Flutter 中的 object - Firebase Documentsnapshot map to Json object in Flutter 使用 Stream 检索数据<querysnapshot>在 Firebase 和 Flutter</querysnapshot> - Retrieving Data with Stream<QuerySnapshot> in Firebase with Flutter containsValue() 不适用于 Flutter 中 Firebase 的 DocumentSnapshot - containsValue() not working with DocumentSnapshot from Firebase in Flutter flutter firebase querysnapshot: dart代码中不区分大小写的方法 - flutter firebase querysnapshot: case insensitive method in dart code QuerySnapshot 优化了 firebase - QuerySnapshot was optimized out firebase Firebase DocumentSnapshot 字段 - Firebase DocumentSnapshot fields Flutter StreamBuilder小部件错误:类型'()=&gt; Map<string, dynamic> ' 不是类型 'DocumentSnapshot 的子类型<object?> ' 在类型转换中</object?></string,> - Flutter StreamBuilder widget Error : type ' () => Map<String, dynamic >' is not a subtype of type 'DocumentSnapshot<Object?>' in type cast 初始化未来<querysnapshot>在 setState Flutter</querysnapshot> - initialize Future<QuerySnapshot> in the setState Flutter 如何作为方法的结果返回 DocumentSnapShot? - How to return a DocumentSnapShot as a result of a method? 相当于 Firebase 实时数据库中的 DocumentSnapshot - Equivalent of DocumentSnapshot in Firebase RealTime Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM