从Firebase提取图像文件数据时出错

时间:2019-10-28 12:32:27

标签: image firebase flutter dart error-handling

我想显示一个文件图像,用户可以从其图库中选择该图像并将其发布为产品图像。然而,香港专业教育学院一直试图显示图像的各种问题,没有任何效果!

我最新的代码更新通过执行此{if(select) print; }来更改我从firebase中获取图像的方式,从而以某种方式消除了由于图像未显示而导致的错误之一,但又出现了另一个错误知道哪个即时解决方案存在问题,它在我的堆栈跟踪中给我以下错误(刷新应用程序并尝试获取通过表单提交的数据后):

image: File(carData['image']),

现在,对此真正奇怪的是,它根本没有调用我的文件。它跳过了这一步,由于找不到图像,因此花费了我在应用程序中内置的“价格” ════════ Exception caught by widgets library ═══════════════════════════════════ The following NoSuchMethodError was thrown building CarItem(dirty, dependencies: [_LocalizationsScope-[GlobalKey#6c638], _InheritedTheme, MediaQuery]): The method '+' was called on null. Receiver: null Tried calling: +("58") 的值。

这是我使用Firebase提取数据:

textFormField

以下是我如何调用显示所有产品的列表中的数据:

Future<void> fetchAndSetCars() async {
    const url = 'https://mylink.firebaseio.com/cars.json';
    try {
      final response = await http.get(url);
      final extractedData = json.decode(response.body) as Map<String, dynamic>;
      final List<AddCar> loadedCars = [];
      extractedData.forEach((carId, carData) {
        loadedCars.add(AddCar(
          id: carId,
          name: carData['name'],
          city: carData['city'],
          country: carData['country'],
          currencyT: carData['currency'],
          price: carData['price'],
          // date: carData['date'],
          sponsNum: carData['sponsNum'],
          category: carData['category'],
          company: carData['company'],
          model: carData['model'],
          year: carData['year'],
          engine: carData['engine'],
          distanceCovered: carData['distanceCovered'],
          transmission: carData['transmission'],
          oilT: carData['oil type'],
          outColor: carData['outter color'],
          inColor: carData['inner color'],
          description: carData['description'],
          address: carData['address'],
          image: File(carData['image']),
        ));
      });
      _cars = loadedCars;
      notifyListeners();
    } catch (error) {
      throw (error);
    }
  }

这是我在CarItem中编码图片显示的方式:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:google_fonts_arabic/fonts.dart';

import '../providers/car_provider.dart';
import '../widget/car_item.dart';

class CarsList extends StatefulWidget {
  @override
  _CarsListState createState() => _CarsListState();
}

class _CarsListState extends State<CarsList> {
    @override
  void initState() {
 Future.delayed(Duration.zero).then((_) {
  Provider.of<Cars>(context).fetchAndSetCars();
 });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final carsData = Provider.of<Cars>(context);
    final car = carsData.cars;
    return car.isEmpty
        ? Center(
            child: Text(
            'error no car found',
            style: TextStyle(
              fontFamily: ArabicFonts.Tajawal,
              fontWeight: FontWeight.bold,
              package: 'google_fonts_arabic',
            ),
          ))
        : ListView.builder(
            padding: const EdgeInsets.only(bottom: 47.0),
            itemCount: car.length,
            itemBuilder: (ctx, i) => CarItem(
              car[i].id,
              car[i].image,
              car[i].name,
              car[i].model,
              car[i].currencyT,
              car[i].price,
              car[i].distanceCovered,
              car[i].transmission,
              car[i].oilT,
              car[i].year,
              car[i].date,
            ),
          );
  }
}

0 个答案:

没有答案