如何在flutter中从Firebase数据库检索数据?

时间:2020-06-24 06:51:42

标签: firebase flutter dart google-cloud-firestore

我正在尝试从Firestore检索数据并将val放入if语句中,但是当我这样做时,它给了我这个错误:

I/flutter ( 8492): Closure: () => String from Function 'toString':.

这是我的代码:

void getUserData() async {
    try {
      firestoreInstance
          .collection('Users')
          .document(usernameController.text)
          .get()
          .then((value) {
        setState(() {
          email = (value.data)['email'];
          password = (value.data)['password'];
          gender = (value.data)['gender'];
          username = (value.data)['username'];
          userType = (value.data)['userType'];
        });
      });
    } catch (e) {
      print(e.toString);
    }
  }

忘了提到当我打印userType这是一个文本时,它会给我NULL。

1 个答案:

答案 0 :(得分:1)

只需删除您的()

void getUserData() async {
try {
  firestoreInstance
      .collection('Users')
      .document(usernameController.text)
      .get()
      .then((value) {
    setState(() {
      email = value.data['email'];
      password = value.data['password'];
      gender = value.data['gender'];
      username = value.data['username'];
      userType = value.data['userType'];
    });
  });
} catch (e) {
  print(e.toString);
}

}

或这样使用

username = value['username'];