firebase - flutter 的Firebase数据检索返回空字符串

标签 firebase flutter dart google-cloud-firestore

当使用_getName()和_getAbout()方法检索数据时,我创建了2个变量(nameUserDisplay和aboutUserDisplay)。返回一个空字符串。
在项目中要求应显示文档中的数据,而不是任何默认数据。
请帮忙。
下面是完整的代码

class UserScreen extends StatefulWidget {
  final User currentUser;

  UserScreen({this.currentUser});

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

class _UserScreenState extends State<UserScreen> {
  String nameUserDisplay;
  String aboutUserDisplay;

  void _getName() async {
    Firestore.instance
        .collection("userInfo")
        .document(widget.currentUser.email)
        .get()
        .then((value) {
      nameUserDisplay = value.data["name"].toString();
      print(nameUserDisplay);
    });
  }

  void _getAbout() async {
    Firestore.instance
        .collection("userInfo")
        .document(widget.currentUser.email)
        .get()
        .then((value) {
      aboutUserDisplay = value.data["about"].toString();
      print(aboutUserDisplay);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getAbout();
    _getName();
  }

  @override
  Widget build(BuildContext context) {
//    if (nameUserDisplay == null) nameUserDisplay = 'Sanatani';

//    if (aboutUserDisplay == null)
//      aboutUserDisplay = 'A Sanatana Dharma Follower';

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        automaticallyImplyLeading: false,
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.white,
            ),
            onPressed: () {
              // do something
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => BlogScreen(
                    currentUser: widget.currentUser,
                  ),
                ),
              );
            },
          )
        ],
        title: Text('You'),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: <Color>[Colors.orange, Colors.red],
            ),
          ),
        ),
      ),
      body: WillPopScope(
        onWillPop: () async {
          return Future.value(false);
        },
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(height: 20),
              Flexible(child: Image.asset('assets/images/hoilogo.png')),
              SizedBox(height: 40),
              Container(
                child: Text(
                  nameUserDisplay,
                  style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                ),
              ),
              SizedBox(height: 20),
              Container(
                padding: EdgeInsets.only(left: 8, right: 8),
                child: Text(
                  aboutUserDisplay,
                  style: TextStyle(fontSize: 15),
                  textAlign: TextAlign.center,
                ),
              ),
              Flexible(child: SizedBox(height: 940)),
              FlatButton(
                child: Text(
                  'Add/Edit your profile Details',
                  style: TextStyle(
                      color: Colors.orange, fontWeight: FontWeight.bold),
                ),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => UserEditDetails(
                        currentUser: widget.currentUser,
                      ),
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Firebase是一个用于创建Google开发的移动和Web应用程序的平台。它最初是一家成立于2011年的独立公司。2014年,Google收购了该平台,现在它已成为其应用程序开发的旗舰产品。
我已将其用作数据库。

最佳答案

获得名称时,您需要调用setState,并要强制使用新的状态值重建小部件。
另外,似乎您可以在同一方法中做两件事以避免调用setState两次,因为它不是免费的。

void _getData() {
    Firestore.instance
        .collection("userInfo")
        .document(widget.currentUser.email)
        .get()
        .then((value) =>
            setState((){
                aboutUserDisplay = value.data["about"].toString();
                nameUserDisplay = value.data["name"].toString();
            }));
}

关于firebase - flutter 的Firebase数据检索返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63219640/

相关文章:

firebase - 如何从Flutter中的TextFormField获取值

android - 为什么Flutter导入提供程序包不起作用?

firebase - 使用 vuejs,如何将 firebase (vuefire) 对象获取到组件数据中

javascript - Firebase - 将对象推送到嵌套的对象列表中

java - android google登录后如何初始化firebase?

flutter - showDialog 有一个关于在 flutter 中调用的方法的错误?

dart - 如何将函数限制为某些参数?

ios - 在真实设备中调用方法 'FirebaseApp.configure()'时,应用程序崩溃

flutter - 异步等待 Navigator.push() - 出现 linter 警告 : use_build_context_synchronously

list - 在 Dart 中合并两个对象列表