You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase实时数据库无效字符报错求助

Firebase实时数据库Invalid character错误解决方案

问题描述

使用Firebase实时数据库时,点击保存按钮触发以下错误:

Invalid character (at character 68) flutter-update-.........europe-west1.firebasedatabase.app/

已移除地址开头的https://,但问题仍未解决。相关Flutter代码如下:

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',
    ),
  ];

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

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

  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);
      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();
  }
}

问题原因

错误出在Uri.https的参数格式上:

  • 第一个参数(域名)末尾多了斜杠/,该参数仅需传入纯域名,无需带路径分隔符
  • 第二个参数(路径)开头多了斜杠/,结合域名的多余斜杠会导致URL解析时出现无效字符

修复方案

修改addProduct方法中的URL构造代码,去掉域名末尾的斜杠和路径开头的斜杠:

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);
    notifyListeners();
  });
}

验证说明

修改后,Uri.https会正确拼接出合法的HTTPS URL:https://flutter-update-ced0c-default-rtdb.europe-west1.firebasedatabase.app/products.json,避免无效字符解析错误。

内容的提问来源于stack exchange,提问作者Tooraj rezaei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 23:04:58