Reputation: 1391
I have the following code which is supposed to update my address record in firebase. However, instead of updating, its adding new fields.
Here's the code:
Future<void> updateAddress (Address address) async{
final url = 'https://m&m.firebaseio.com/address/$userId.json?auth=$authToken';
await http.patch(url, body:json.encode({
'phoneNumber':address.phoneNumber,
'name':address.name,
'details':address.details,
'alternatePhone':address.alternatePhone
}));
_items.first = address;
notifyListeners();
}
Here's how it looks after I execute the update function:
Upvotes: 2
Views: 63
Reputation: 469
Hey I see that you are updating all the values of user so instead of patch you can also use http.put
like this
Future<void> updateAddress (Address address) async{
final url = 'https://m&m.firebaseio.com/address/$userId.json?auth=$authToken';
await http.put(url, body:json.encode({
'phoneNumber':address.phoneNumber,
'name':address.name,
'details':address.details,
'alternatePhone':address.alternatePhone
}));
_items.first = address;
notifyListeners();
}
also refer these docs for more info firebase realtime database docs
feel free to comment and ask more
Upvotes: 1
Reputation: 4081
// Create an initial document reference to update:
var docRef = db.collection("address").doc("$authToken");
docRef.set({
name: "Frank",
favorites: { alternatePhone: "125478848844", details: "212 jenn", name: "James", phoneNumber: "2142889776" },
age: 12
});
// To update fields in nested objects:
db.collection("address").doc("$authToken").update({
"alternatePhone": 125478848844,
"details": "212 jenn",
"name": "James"
});
Upvotes: 1