0

I am trying to do a simple post request from a flutter app to a firebase database. This is my code:

child: RaisedButton(
  child:Text('A firebase'),
  textColor: Colors.blueGrey,
  onPressed: () {
    var url = "https://gip-reports.firebaseio.com/reporte.json";
    http.post(url, body: {"name": "doodle", "color": "blue"})
      .then((response) {
         print("Response status: ${response.statusCode}");
         print("Response body: ${response.body}");
       });
  },
),

The problem is, when I use this code, the cmd shows this error:

I/flutter (16602): Response status: 401
I/flutter (16602): Response body: {
I/flutter (16602):   "error" : "Permission denied"
I/flutter (16602): }

How can I get my app to post to firebase?

DevTard
  • 464
  • 3
  • 11
Neto Paez
  • 113
  • 1
  • 1
  • 8

2 Answers2

0

The answer was easy. I did not know about the default pemissions of realtime databases of firebase. The default permissions are:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

But if I want to try doing http request in the development time, I can change the permissions to:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

You can change this in the database section of firebase -> rules

I got the answer thanks to this post Firebase Permission Denied

Neto Paez
  • 113
  • 1
  • 1
  • 8
0

Setting permissions to

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

would work.

The best way of doing this however, would be to authenticate your users using the Firebase Auth package: Firebase_Auth package (even if you sign them in anonymously) and then to use the Firebase_Database package: Firebase_Database Package to load data in realtime.

DevTard
  • 464
  • 3
  • 11