Home > Net >  Flutter Firestore onPressed not writing Future
Flutter Firestore onPressed not writing Future

Time:11-23

I'm trying to call a future inside of my widget tree to write my userSetup class to my Firestore database. I get no errors running my code but it does not execute the function and nothing gets written to my database. I was wondering how to rectify this so when I press a button, it would write to my database

Here is my userSetup:

Future<void> userSetup(final String username, final String displayName,
    final String profilePhoto, final String bio, final String bioLink) async {
  CollectionReference users = FirebaseFirestore.instance.collection('users');
  FirebaseAuth auth = FirebaseAuth.instance;
  String uid = auth.currentUser!.uid.toString();
  await users.add({
    'uid': uid,
    'displayName': displayName,
    'username': username,
    'profilePhoto': profilePhoto,
    'bio': bio,
    'bioLink': bioLink
  });
  return;
}

Here is my button:

ElevatedButton(
     style: TextButton.styleFrom(
        backgroundColor: Colors.blue,
        ),
      onPressed: () => userSetup(username, displayName, profilePhoto, bio, bioLink),

CodePudding user response:

Try this:

Future userSetup(final String username, final String displayName,
    final String profilePhoto, final String bio, final String bioLink) async {
    
  CollectionReference users = FirebaseFirestore.instance.collection('users');
  FirebaseAuth auth = FirebaseAuth.instance;
  String uid = auth.currentUser!.uid.toString();

  return await users.add({
    'uid': uid,
    'displayName': displayName,
    'username': username,
    'profilePhoto': profilePhoto,
    'bio': bio,
    'bioLink': bioLink
  });

 

edit: Try creating an object and then pass it as a parameter to your function:

class UserInfo {
 final String username;
 final String displayName;
 ...
 }

  • Related