【问题标题】:Firebase data retrieval in flutter returns a null stringFlutter 中的 Firebase 数据检索返回一个空字符串
【发布时间】:2020-08-02 18:25:46
【问题描述】:

当使用 _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 开发的用于创建移动和网络应用程序的平台。它最初是一家成立于 2011 年的独立公司。2014 年,谷歌收购了该平台,现在它是他们用于应用程序开发的旗舰产品。 我已将其用作数据库。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    当您获得名称并即将强制使用新状态值重建小部件时,您需要调用 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();
                }));
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-03
      • 2017-10-10
      • 2013-01-17
      • 2019-10-29
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多