到目前为止,在我的应用程序中,一切正常,除了我不断收到的一个错误:
type 'String' is not a subtype of type 'File'
我尝试了很多方法来尝试解决问题,但尚未解决任何问题。
我可以理解问题出在哪里,但我无法通过无数次尝试来解决它。
问题是我传递了一个Image
使用ImagePicker gallery
im 将图像数据传递给 firebaseimage: image.toString()
并且它工作正常。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,
});
}
火力基地数据:
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)