简体   繁体   中英

Getting Data from cloud Firestore and showing for flutter app

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();
  }

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@gmail.com";
    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.

This error is because, You should initialize your user variable like var users = {}; instead of var user;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM