“字符串”类型不是“文件”类型的子类型

时间:2019-10-28 10:34:18

标签: firebase file flutter error-handling imagepicker

到目前为止,在我的应用程序中一切正常,除了我不断遇到的一个错误:

type 'String' is not a subtype of type 'File'

我尝试了多种方法来修复此问题,但仍未解决。

我可以理解问题出在哪里,但无数次尝试都无法解决。

问题在于,我使用Image传递了ImagePicker gallery并以image: image.toString()的形式将图像数据传递到了Firebase,并且运行良好。 Firebase会采用该路径,但由于出现错误,我得到了:_file != null,因为图像确实是File image,所以我无法从firebase中获取数据并将字符串路径作为参数传递。因此出现此错误type 'String' is not a subtype of type 'File'。我像下面的Image.file(image)一样在应用程序上显示图像,因为这是显示文件图像并使用ImagePicker的唯一方法。有解决方案吗?还是做一个试图实现的想法的坏方法?

代码如下:

图像选择器:

String img;

  static Future<String> fileToB64(File f) async {
    List<int> imageBytes = f.readAsBytesSync();

    return base64Encode(
      imageBytes,
    );
  }

  Future<void> _takePicture() async {
    final imageFile = await ImagePicker.pickImage(
      source: ImageSource.gallery,
    );
    setState(() {
      data.image = imageFile;
    });
    fileToB64(imageFile).then((d) {
      setState(() {
        img = d; //base64Decode(d);
      });
    });
  }

提供者:

import: 'dart:io';

class AddCar {
  // other data
  File image;

  AddCar({
    this.// other data
    this.image,
  });
}

firebase数据:

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(
          // other data
          image: carData['image'],
        ));
      });
      _cars = loadedCars;
      notifyListeners();
    } catch (error) {
      throw (error);
    }
  }

  AddCar findById(String id) {
    return _cars.firstWhere((carProd) => carProd.id == id);
  }

  void addCar(AddCar car) {
    const url = 'https://mylink.firebaseio.com/cars.json';
    http.post(
      url,
      body: json.encode({
        // other data
        'image': car.image.toString(),
      }),
    );
    final newCar = AddCar(
    // other data
      image: car.image,
    );
    _cars.insert(0, newCar); // add car at the top of the list
    notifyListeners();
  }

我如何显示从Firebase提取的数据:

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

我如何调用要在应用程序中显示的数据:

Container(
                width: MediaQuery.of(context).size.width * 0.35,
                height: MediaQuery.of(context).size.width * 0.35,
                child: GestureDetector(
                  child: Image.file(
                    image,
                    fit: BoxFit.fill,
                  ),
                  onTap: () {
                    Navigator.of(context).pushNamed(
                      MyCarDetails.routeName,
                      arguments: id,
                    );
                  },
                ),
              ),

我运行该应用程序后会得到什么:

Restarted application in 6,085ms.
E/flutter ( 3497): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'String' is not a subtype of type 'File'
E/flutter ( 3497): #0      Cars.fetchAndSetCars 
package:flutter_app/providers/car_provider.dart:54
E/flutter ( 3497): <asynchronous suspension>
E/flutter ( 3497): #1      _CarAreaState.initState.<anonymous closure> 
package:flutter_app/home_parts/cars_area.dart:28
E/flutter ( 3497): #2      _rootRunUnary  (dart:async/zone.dart:1132:38)
E/flutter ( 3497): #3      _CustomZone.runUnary  (dart:async/zone.dart:1029:19)
E/flutter ( 3497): #4      _FutureListener.handleValue  (dart:async/future_impl.dart:137:18)
E/flutter ( 3497): #5      Future._propagateToListeners.handleValueCallback  (dart:async/future_impl.dart:678:45)
E/flutter ( 3497): #6      Future._propagateToListeners  (dart:async/future_impl.dart:707:32)
E/flutter ( 3497): #7      Future._complete  (dart:async/future_impl.dart:512:7)
E/flutter ( 3497): #9      _rootRun  (dart:async/zone.dart:1120:38)
E/flutter ( 3497): #10     _CustomZone.run  (dart:async/zone.dart:1021:19)
E/flutter ( 3497): #11     _CustomZone.runGuarded  (dart:async/zone.dart:923:7)
E/flutter ( 3497): #12     _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:963:23)
E/flutter ( 3497): #13     _rootRun  (dart:async/zone.dart:1124:13)
E/flutter ( 3497): #14     _CustomZone.run  (dart:async/zone.dart:1021:19)
E/flutter ( 3497): #15     _CustomZone.bindCallback.<anonymous closure>  (dart:async/zone.dart:947:23)
E/flutter ( 3497): #16     Timer._createTimer.<anonymous closure>  (dart:async-patch/timer_patch.dart:21:15)
E/flutter ( 3497): #17     _Timer._runTimers  (dart:isolate-patch/timer_impl.dart:382:19)
E/flutter ( 3497): #18     _Timer._handleMessage  (dart:isolate-patch/timer_impl.dart:416:5)
E/flutter ( 3497): #19     _RawReceivePortImpl._handleMessage  (dart:isolate-patch/isolate_patch.dart:172:12)

1 个答案:

答案 0 :(得分:2)

似乎不是这样做:

image: carData['image']

您应该这样做:

image: File(carData['image'])

因为carData['image']String,而不是File,并且AddCar期望File