使用FutureBuilder获得Future< String>从消防站 [英] using FutureBuilder to get Future<String> from firestore

查看:136
本文介绍了使用FutureBuilder获得Future< String>从消防站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我想在其中显示一个Future电子邮件,但是我将从Firestore中获取它。但是,我不确定我将如何需要使用FutureBuilder检索我的值

This is my code in which I want to display an email which is a Future and I will get it from my firestore.However, I am not sure how I will need to retrieve my value using FutureBuilder which I want to use as a string.

这是我获取电子邮件的方法:

This is my method to get my email:

Future<String> getEmail() async {
    String _email = (await FirebaseAuth.instance.currentUser()).email;
    DocumentSnapshot snapshot = await _firestore.collection('users')
      .document(_email)
      .collection('met_with')
      .document('email')
      .get();
    // print("data: ${snapshot.data}"); // might be useful to check
    return snapshot.data['email']; // or another key, depending on how it's saved
  }

这是我更新的代码:

      @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 3.0,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
      child: ListTile(
        leading: CircleAvatar(
          backgroundImage: AssetImage(imagePath),
        ),
        trailing: Icon(Icons.more_horiz),
        title: Text(
          email,
          style: TextStyle(
            c  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 3.0,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
      child: ListTile(
        leading: CircleAvatar(
          backgroundImage: AssetImage(imagePath),
        ),
        trailing: Icon(Icons.more_horiz),
        title: Text(
          email,
          style: TextStyle(
            color: Colors.deepPurple[700],
            fontWeight: FontWeight.bold,
          ),
        ),
        subtitle: Text(infection),
        onTap: () => showModalBottomSheet(
            context: context,
            builder: (builder) {

              return FutureBuilder(
                future: getEmail(),
                builder: (BuildContext context, snapshot) {
                if(snapshot.hasData){
                  if(snapshot.connectionState == ConnectionState.waiting){
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }else{
                    return Padding(padding: EdgeInsets.symmetric(vertical: 50.0, horizontal: 10.0),
                     child: Column(
                  children: <Widget>[
                    BottomSheetText(
                        question: 'Email', result:  snapshot.data['email']),
                    SizedBox(height: 5.0),
                    BottomSheetText(
                        question: 'Contact Time',result:"lol"),// getTimeStamp().toString()),
                    SizedBox(height: 5.0),
                    BottomSheetText(
                        question: 'Contact Location',
                        result: "help"),
                    SizedBox(height: 5.0),
                    BottomSheetText(question: 'Times Contacted', result: "lool",),
                  ],
                  ),


                     );
                  }
                }else{
                  return CircularProgressIndicator();}
                }








                     );




            }
        ),





                ),
              );



  }
}

这是我的Firebase数据库:

Here is my firebase database:

在此处输入图像描述

推荐答案

您的查询错误,请尝试以下操作。

Your query is wrong, try following one.

Future<String> getEmail() async {
    String _email = (await FirebaseAuth.instance.currentUser()).email;
    var a = await Firestore.instance
            .collection("met_with")
            .where('email', isEqualTo:.  _email )
            .getDocuments();
     return a.documents[0]['email'];
}

要调用此方法,您需要futureBuilder。

And to call this method you need futureBuilder.

FutureBuilder(
            future: getEmail(),
            builder: (BuildContext context, snapshot) {
            if(snapshot.hasData){
              if(snapshot.connectionState == ConnectionState.waiting){
                return Center(
                  child: CircularProgressIndicator(),
                );
              }else{
               return Center( // here only return is missing
                  child: Text(snapshot.data['email'])
                );
              }
            }else if (snapshot.hasError){
              return Text('no data');
            }
              return CircularProgressIndicator();
            },
          ),

这篇关于使用FutureBuilder获得Future&lt; String&gt;从消防站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆