William
William

Reputation: 974

Filter Firestore updating document

My app keeps crashing when I try to update a document. What am I doing wrong?

 Future<bool> updateEvent(data, id) async {
    try {
      final _ref = _db.collection('content').doc(id);
      print(_ref);
      await _ref.update(data);

      return true;
    } catch (error) {
      print(error);
      return false;
    }
  }

print(_ref) returns

flutter: DocumentReference(content/KwaKlb1GpUbqSQd8K9rd)

data is set like this

final data = {
      "active": true,
      "title": _titleTextController.text != null
          ? _titleTextController.text
          : null,
  ...
};

Upvotes: 0

Views: 29

Answers (2)

ahmetakil
ahmetakil

Reputation: 908

final data = {
  "active": true,
  if(_titleTextController.text != null)
  "title": _titleTextController.text
      
 };

Should only add the field if it's not null or you can use ?? to assign a default value

Upvotes: 1

To answer properly, it would be good to see your error message. I don't currently use Firestore with the Flutter, but if it works like Firestore on other platforms, it will not allow setting a Null -value to a field. This will happen in your example when the text is empty.

Upvotes: 1

Related Questions