看来我对Firebase数据库的真实数据库有问题.

该数据库的地址为flutter-update-.........europe-west1.firebasedatabase.app/

当我点击保存按钮时,错误:

无效字符(位于字符68)flutter-update-.........europe-west1.firebasedatabase.app/

我甚至删除了https://从开始的数据库地址,但不工作!

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:my_shop/providers/product.dart';

class Products with ChangeNotifier {
  final 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',
    ),
  ];

  //var _showFavoritesOnly = false;
  List<Product> get items {
    // if (_showFavoritesOnly) {
    //   return _items.where((prodItem) => prodItem.isFavorite).toList();
    // }
    return [..._items];
  }

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

  // void showFavoritesOnly() {
  //   _showFavoritesOnly = true;
  //   notifyListeners();
  // }

  // void showAll(){
  //   _showFavoritesOnly = false;
  //   notifyListeners();
  // }

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

  void addProduct(Product product) {
    var url = Uri.https(
        'flutter-update-ced0c-default-rtdb.europe-west1.firebasedatabase.app/',
        '/products.json');
    http
        .post(
      url,
      body: json.encode({
        'title': product.title,
        'description': product.description,
        'price': product.price,
        'imageUrl': product.imageUrl,
        'isFavorite': product.isFavorite
      }),
    )
        .then((response) {
      final newProduct = Product(
          id: json.decode(response.body)['name'],
          title: product.title,
          description: product.description,
          price: product.price,
          imageUrl: product.imageUrl);
      _items.add(newProduct);
      //_items.insert(0,newProduct);
      notifyListeners();
    });
  }

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

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

推荐答案

你有一个无效的结尾字符串,/.

  var url = Uri.https(
        'flutter-update-ced0c-default-rtdb.europe-west1.firebasedatabase.app',
        '/products.json');

您也可以查看parse method

var url = Uri.parse('https://flutter-update-ced0c-default-rtdb.europe-west1.firebasedatabase.app/products.json');

Flutter相关问答推荐

如何在容器中移动图标按钮?

在fltter_widget_from_html中启动url

在Flutter 中创建自定义Snackbar类

Flutter 图标记移动

为什么PUT方法没有发出任何响应?

我如何在Android 13中通过Flutter应用程序打开文件(不是图像、视频或音频)?

如何在 Flutter 中以背景 colored颜色 制作圆圈的一部分

带按钮的 Riverpod future Provider

无状态和有状态小部件,我可以同时使用吗?

hooks_riverpod 有用途吗

如何使用Riverpod提供程序正确构建搜索栏?

Flutter ImagePicker - 在 onPressed 中异步获取图像显示 lint 警告

减少 Flutter 中 API 的连接等待时间

如何修复 ICU Lexing 错误:Flutter 中的意外字符

无效的 depfile: C:\xxx\xxx\Flutter\project_name\.dart_tool\flutter_build\df4b308df1ee4bce22c56c71751554d1\kernel_snapshot.d 无效

如何在 flutter 中制作这种类型的扩展瓦片

如何在Flutter 中水平滚动堆叠定位的小部件?

如何将所有行项目平等地放在一行中?Flutter

将容器扩展到安全区域 - Flutter

在右侧从 a 到 z Flutter 字母列表