Mike Keller
Mike Keller

Reputation: 65

Adding data to array within a firebase document (flutter/ flutterfire)

I'm trying to add vehicles to an array within a firebase document using Flutter and Flutterfire. The vehicle data is constantly overwritten or only a "Processing Data" snack bar appears. How can I get this vehicle to be added to the array?

Future<bool> _saveCar() async {
bool output = false;

_carCollection!.doc(widget.appState.profile!.uId).update({
  'car': FieldValue.arrayUnion([
    {
      "licensePlate": licensePlate.text,
      "brand": "",
      "model": "",
      "weight": "",
      "height": "",
      "length": "",
      "powerNeeded": "",
      "effluenceNeeded": "",
    },
  ]),
}).then((value) {
  print("Car Added");
  output = true;
}).catchError((error) => print("Failed to add car: $error"));

return output;

} }

Upvotes: 1

Views: 133

Answers (1)

Gbenga B Ayannuga
Gbenga B Ayannuga

Reputation: 2792

try this, set your merge to true

docRef.set(
  { "car": FieldValue.arrayUnion([locationMap])}, 
  SetOptions(merge : true)
)

Upvotes: 1

Related Questions