skip to Main Content

I have the following code. The idea is that it will create a record in the groups collection. If successful, then it should create a record in a different collection – the groups_UID collection. Is this the correct way to do this? I feel like it might be wrong because async/await are normally used separately from .then(), right? Or maybe I’m just misremembering something here.

await docRef.set({
      'adminUID': currentUID,
      'adminUsername': currentUsername,
      'createDate': timestamp,
      'groupID': groupID,
      'groupName': groupName,
    }).then((value) async {
      print('##MyApp## database createGroupFS groups record added');

      CollectionReference groupsUIDCollection = firestore.collection('groups_' + currentUID);
      final groupsUIDDocRef = groupsUIDCollection.doc();

      await groupsUIDDocRef.set({
        'adminUID': currentUID,
        'adminUsername': currentUsername,
        'createDate': timestamp,
        'groupID': groupID,
        'groupName': groupName,
      }).then((value) {
        print('##MyApp## database createGroupFS groups_UID record added');
        returnVal = groupID;
      }).catchError((error) {
        print('##MyApp## database createGroupFS groups_UID ERROR: ' + error.toString());
        returnVal = '';
      });
    }).catchError((error) {
      print('##MyApp## database createGroupFS groups ERROR: ' + error.toString());
      returnVal = '';
    });

2

Answers


  1. You should either use one or another. I prefer using async/await with try/catch because I find it more readable. So in your example solution would be:

    try {
      // if it fails it goes to the catch part
      final res = await docRef.set({
        'adminUID': currentUID,
        'adminUsername': currentUsername,
        'createDate': timestamp,
        'groupID': groupID,
        'groupName': groupName,
      });
      // it will continue going on success
      print('##MyApp## database createGroupFS groups record added');
    
      CollectionReference groupsUIDCollection =
          firestore.collection('groups_' + currentUID);
      final groupsUIDDocRef = groupsUIDCollection.doc();
    
      try {
        // same thing here
        final res2 = await groupsUIDDocRef.set({
          'adminUID': currentUID,
          'adminUsername': currentUsername,
          'createDate': timestamp,
          'groupID': groupID,
          'groupName': groupName,
        });
        // success
        print('##MyApp## database createGroupFS groups_UID record added');
        returnVal = groupID;
      } catch (error) {
        // error happened on groupsUIDDocRef.set
        print('##MyApp## database createGroupFS groups_UID ERROR: ' +
            error.toString());
        returnVal = '';
      }
    } catch (error) {
      // error happened on docRef.set
      print(
          '##MyApp## database createGroupFS groups ERROR: ' + error.toString());
      returnVal = '';
    }
    
    Login or Signup to reply.
  2. you should use try/catch as it will give you more flexibility when the awaited function throws error.

    the syntax should be,

    try{
    var result= await someFunction();
    //perform action 
    }catch(e,stacktrace){
    //handle error
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search