0

I have the following function:

 Future<void> addLoyalty(int points) async {
    print('inside add loyalty'); 
    final url =
        'https://fnj11223.firebaseio.com/loyalty/$userId.json?auth=$authToken';
    try {
        await http.post(
        url,
        body: json.encode({
          'points': points,
        }),
      );
    } catch (error) {
      print(error);
      throw error;
    }
  }

Here's the database snapshot:

enter image description here

I want to get rid of the extra ID generated. How can I tweak my function to achieve this?

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
mcfred
  • 1,183
  • 2
  • 29
  • 68

2 Answers2

2

You are making a POST request which is equivalent to the push() method in Firebase Client SDKs. If you want to write at a specific location without the push key, make a PUT request as mentioned in the documentation.

Sample request from the docs:

curl -X PUT -d '{ "first": "Jack", "last": "Sparrow" }' \
  'https://[PROJECT_ID].firebaseio.com/users/jack/name.json'
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • put did it for me. Thanks alot. – mcfred Jul 19 '21 at 17:46
  • but, when u used PUT. it can't add a new item. but, it makes updating the oldest data into the newest data and it makes me stuck if we found any case that needs to show all data lists without a unique id. – Michael Fernando Oct 05 '22 at 13:12
0

Insert firebase real time plugin to your app pubspec.yaml file instead of calling APIs. As Plugin gives you advance feature and easy to integrate functionality https://pub.dev/packages/firebase_database/install

Instead of adding just set values to your reference.

like dbRef.set({your json goes here})

Nik
  • 1,991
  • 2
  • 13
  • 30