简体   繁体   中英

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

I have a future builder which is returning the name, however I am querying the userMood table to return the last created document.

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. by the way I cannot where you are calling getMood() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM