对Firebase数据库API的Flutter http发布请求导致404

时间:2019-07-26 15:10:24

标签: firebase http flutter dart

我正在尝试在flutter项目中使用firebase实时数据库API。尽管通过http包中的addProduct包向API发出POST请求,但总是在import 'package:scoped_model/scoped_model.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import '../models/product.dart'; class ProductsModel extends Model{ final String base_url = 'https://PROJECT_ID.firebaseio.com/'; List<Product> _products = []; int _selectedProductIndex; bool _showFavorites = false; List<Product> get products { return List.from(_products); } List<Product> get displayedProducts { return List.from(_products.where((Product p){ return p.isFavorite == _showFavorites; })); } int get selectedProductIndex{ return _selectedProductIndex; } Product get selectedProduct{ return _selectedProductIndex == null ? null : _products[_selectedProductIndex]; } bool get displayFavoritesOnly{ return _showFavorites; } void addProduct(Product product) { final Map<String, dynamic> productData = { 'title': product.title, 'description': product.description, 'price': product.price, 'imageUri': "http://lorempixel.com/640/480/food", 'userEmail': product.userEmail, 'userId': product.userId }; var data = jsonEncode(productData); http.post('https://flutter-course-36df8.firebaseio.com/' + 'products.josn', body: data, headers: { "Accept": "application/json", }).then((http.Response response){ // final Map<String, dynamic> d = json.decode(response.body); print(response.statusCode);// always prints 404 print('test'); _products.add(product); _selectedProductIndex = null; }); } void deleteProduct(){ _products.removeAt(_selectedProductIndex); _selectedProductIndex = null; } void updateProduct(Product product){ _products[_selectedProductIndex] = product; _selectedProductIndex = null; } void selectProduct(int index){ _selectedProductIndex = index; notifyListeners(); } void toggleProductFavoriteStatus(){ final bool isCurrentlyFavorite = selectedProduct.isFavorite; final bool newFavoriteStatus = ! isCurrentlyFavorite; final Product updatedProduct = Product( title: selectedProduct.title, description: selectedProduct.description, price: selectedProduct.price, imageUri: selectedProduct.imageUri, userEmail: selectedProduct.userEmail, userId: selectedProduct.userId, isFavorite: newFavoriteStatus ); _products[_selectedProductIndex] = updatedProduct; _selectedProductIndex = null; notifyListeners(); } void toggleDisplayMode(){ _showFavorites = ! _showFavorites; notifyListeners(); } } 方法中返回状态代码404,但奇怪的是,当使用同一API Url通过Postman发出POST请求时有用。我不知道问题出在哪里。任何帮助,将不胜感激。 我发出POST请求的模型类

DSL.field(Name)

0 个答案:

没有答案