这网址显示错误给我铸造错误.我正在使用实时数据库和我也使用
{
"rules": {
"Products": {
".read": "auth != null",
".write": "auth != null"
}
}
}
当我添加数据时,数据保存在应用程序中,但post request未将其保存在firebase中我创建此应用程序表单acedemine course. URL正确。该URL可从您的我的位置或网络访问。服务器正在运行或维护。我想解决此问题,
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();
}
}
3条答案
按热度按时间ngynwnxp1#
您的数据库安全规则要求有一个经过身份验证的用户(
"auth != null"
)才能访问数据。但是在
http.post
调用中,您没有指定任何身份验证凭证,这意味着它是一个匿名用户,因此安全规则正确地拒绝了该请求。要使
http.post
调用来自经过身份验证的用户,您需要传递一个令牌,如authenticating REST requests文档中所示。gcuhipw92#
Firebase不允许使用
http
这样的外部软件包来发送请求。您应该使用firebase_database
API。文档链接如下:https://firebase.flutter.dev/docs/database/startu5i3ibmn3#
我问chatgpt它解决我的答案。我只是改变规则。
然后我根据chatgpt修改代码