flutter中的Firebase数据检索返回一个空字符串



当使用_getName((&amp_getAbout((方法。返回一个空字符串。

项目中要求显示文档中的数据,而不是任何默认数据。

请帮忙。

以下是完整的代码

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

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

相关内容

最新更新