【问题标题】:What is the best way to do a simple post request from flutter app to firebase?从flutter应用程序到firebase的简单发布请求的最佳方法是什么?
【发布时间】:2019-08-17 01:05:57
【问题描述】:

我正在尝试从 Flutter 应用程序向 Firebase 数据库发出简单的发布请求。 这是我的代码:

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}");
       });
  },
),

问题是,当我使用这段代码时,cmd显示这个错误:

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

如何让我的应用发布到 Firebase?

【问题讨论】:

标签: http firebase-realtime-database flutter


【解决方案1】:

设置权限

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

会工作的。

但是,最好的方法是使用 Firebase Auth 包对您的用户进行身份验证:Firebase_Auth package(即使您匿名登录),然后使用 Firebase_Database 包:Firebase_Database Package 加载数据即时的。

【讨论】:

    【解决方案2】:

    答案很简单。我不知道 firebase 实时数据库的默认权限。默认权限为:

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

    但如果我想在开发时尝试做http请求,我可以将权限更改为:

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

    您可以在 firebase 的数据库部分进行更改 -> 规则

    感谢Firebase Permission Denied这个帖子,我得到了答案

    【讨论】:

      猜你喜欢
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多