Firebase在Flutter应用中未添加数据。

huangapple go评论63阅读模式
英文:

firebase didnt add data using flutter app

问题

抱歉,您提供的内容似乎包含一些代码和特殊字符,无法完全理解。以下是您要翻译的文本的翻译部分:

"the url showing error giving me cast error. i am using realtime database and i also use"(显示错误的URL给我抛出了类型错误。我正在使用实时数据库,同时也使用)

"when i add data the data is being saved in the app but post request didn't save it in the firebase"(当我添加数据时,数据保存在应用程序中,但POST请求没有将其保存在Firebase中)

"The URL is correct .The URL is accessible from your my location or network.The server is running or maintenance. i want to resolve this issue,"(URL是正确的。该URL可以从您的位置或网络访问。服务器正在运行或维护中。我想解决这个问题。)

"import 'dart:convert';"(导入'dart:convert';)

"import 'package:flutter/material.dart';"(导入'package:flutter/material.dart';)

"import 'package:http/http.dart' as http;"(导入'package:http/http.dart' as http;)

"import './product.dart';"(导入'./product.dart';)

"class Products with ChangeNotifier {"(类Products,带有ChangeNotifier)

"List _items = ["(_items列表包含Product对象)

"Product findById(String id) {"(通过ID查找产品的方法)

"void addProduct(Product product) {"(添加产品的方法)

"const url = "https://flutter-update-'this place is hide by me'firebasedatabase.app/Products.json";"(URL地址)

"void updateProduct(String id, Product newProduct) {"(更新产品的方法)

"void deleteProduct(String id) {"(删除产品的方法)

请注意,由于代码和特殊字符的存在,一些文本可能不够清晰或不完整。如果您需要更多帮助,请提供更具体的问题或文本。

英文:

the url showing error giving me cast error. i am using realtime database and i also use

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

  }
}

when i add data the data is being saved in the app but post request didn't save it in the firebase
i creating this app form acedemine course.
The URL is correct .The URL is accessible from your my location or network.The server is running or maintenance. i want to resolve this issue,

  import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

import './product.dart';

class Products with ChangeNotifier {
  List<Product> _items = [
    Product(
      id: 'p1',
      title: 'Red Shirt',
      description: 'A red shirt - it is pretty red!',
      price: 29.99,
      imageUrl:
          'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
    ),
    Product(
      id: 'p2',
      title: 'Trousers',
      description: 'A nice pair of trousers.',
      price: 59.99,
      imageUrl:
          'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Trousers%2C_dress_%28AM_1960.022-8%29.jpg/512px-Trousers%2C_dress_%28AM_1960.022-8%29.jpg',
    ),
    Product(
      id: 'p3',
      title: 'Yellow Scarf',
      description: 'Warm and cozy - exactly what you need for the winter.',
      price: 19.99,
      imageUrl:
          'https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg',
    ),
    Product(
      id: 'p4',
      title: 'A Pan',
      description: 'Prepare any meal you want.',
      price: 49.99,
      imageUrl:
          'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg',
    ),
  ];
 

  List<Product> get items {
 
    return [..._items];
  }

  List<Product> get favoriteItems {
    return _items.where((prodItem) => prodItem.isFavorite).toList();
  }

  get id => null;

  Product findById(String id) {
    return _items.firstWhere((prod) => prod.id == id);
  }



  void addProduct(Product product) {
    const url =
        "https://flutter-update-'this place is hide by me'firebasedatabase.app/Products.json";
    http.post(
      url ,
      body: json.encode({
        'title ': product.title,
        'description': product.description,
        'imageUrl': product.imageUrl,
        'price': product.price,
        'isFavorite': product.isFavorite
      }),
    );

    final newProduct = Product(
      title: product.title,
      description: product.description,
      price: product.price,
      imageUrl: product.imageUrl,
      id: DateTime.now().toString(),
    );
    _items.add(newProduct);

    notifyListeners();
  }

  void updateProduct(String id, Product newProduct) {
    final prodIndex = _items.indexWhere((prod) => prod.id == id);
    if (prodIndex >= 0) {
      _items[prodIndex] = newProduct;
      notifyListeners();
    } else {
      print('...');
    }
  }

  void deleteProduct(String id) {
    _items.removeWhere((prod) => prod.id == id);
    notifyListeners();
  }
}

答案1

得分: 1

你的数据库安全规则要求必须有经过身份验证的用户("auth != null")才能访问数据。

但是,在你的 http.post 调用中,你没有指定任何身份验证凭据,这意味着它是一个匿名用户。因此,安全规则正确地拒绝了该请求。

要使 http.post 调用来自经过身份验证的用户,你需要按照REST请求身份验证文档中所示传递一个令牌。

英文:

Your database security rules require that there is an authenticated user ("auth != null") in order to access the data.

But in your http.post call you don't specify any authentication credentials, which means that it is an anonymous user. So the security rules correctly reject that request.

To make http.post call come from an authenticated user, you'll need to pass a token as shown in the documentation on authenticating REST requests.

答案2

得分: 0

Firebase不允许使用外部包(如http)进行POST请求。您应该改用firebase_database API。文档链接在这里:https://firebase.flutter.dev/docs/database/start

英文:

Firebase is not allowing post requests using external packages such as http. You should instead use the firebase_database api. Link for the documentation here: https://firebase.flutter.dev/docs/database/start

huangapple
  • 本文由 发表于 2023年3月9日 17:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定