Reputation: 705
I have a profile screen. and getting data from the cloud store and showing in the profile screen.
I guess there is no problem while retrieving data but the problem is while showing. I don't know how I mess up? Now the error is only showing "Loading" Text. Help me
class Profile extends StatefulWidget {
@override
_ProfileState createState() => _ProfileState();
}
class _ProfileState extends State<Profile> {
bool userFlag = false;
var users;
@override
void initState() {
// TODO: implement initState
super.initState();
UserManagement().getData().then((QuerySnapshot docs){
userFlag = true;
users = docs.documents[0].data;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Profile'),
),
body: Container(
padding: EdgeInsets.all(50),
child: Column(
children:<Widget>[
name(),
],
),
),
);
}
Widget name() {
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
"Name",
style: TextStyle(fontWeight: FontWeight.w600,fontSize: 18),
),
SizedBox(
width: 45,
),
userFlag ? Text(users['Name'],
style: TextStyle(fontWeight: FontWeight.w400,fontSize: 18),
)
:Text('Loading'),
],
),
);
}
for getting Data i have:
getData(){
return Firestore.instance
.collection('users').getDocuments();
}
Upvotes: 0
Views: 60
Reputation: 21
I get and Use Data that way:
QuerySnapshot Users = await _fs
.collection("users")
.getDocuments();
It will give you all the users in the users collection. so for retrieving one user in specific I use a "for loop".
String myEmail = "[email protected]";
String username;
for (var user in users.documents) {
if ( myEmail == user.data["email"]){
// you have all the field for the user using "myEmail".
username = user.data["username"];
} else {
print("There is no User with this email");
}
}
But I think there might be a better way to do it.
Upvotes: 1
Reputation: 2436
This error is because, You should initialize your user variable like
var users = {};
instead of
var user;
Upvotes: 0