flutterdart

How to add try/catch to Future<void> in flutter


I am trying to debug the save function below. I tried to add a try/catch but in the catch I don't know how to return a null for a Future.

As I step through the code I get to the .doc(currentCleintId) and it exits. The currentClientid is the correct value so I don't know why it would fail.

  Future<void> saveClient(client, String currentClientid) {
    return _db.collection('client').doc(currentClientid).set(client);
  }

Thanks in advance


Solution

  • Does this work for you?

     Future<void> saveClient(client, String currentClientid) async {
        try {
          await _db.collection('client').doc(currentClientid).set(client);
        } catch(e){
           print(e.toString();
        }
     }