类型 'String' 不是类型 'File' 的子类型
type 'String' is not a subtype of type 'File'
到目前为止,我的应用程序一切正常,除了我不断收到一个错误:
type 'String' is not a subtype of type 'File'
我尝试了很多方法来尝试解决问题,但仍未解决任何问题。
我能理解问题出在哪里,但我尝试了无数次都无法解决它。
问题是我使用 ImagePicker gallery
传递一个 Image
,我将该图像数据作为 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)
似乎不是这样做:
image: carData['image']
你应该这样做:
image: File(carData['image'])
因为 carData['image']
是 String
,而不是 File
,并且 AddCar
需要 File
。
有时是因为使用 !空安全你应该 运行 喜欢 flutter 运行 --no-sound-null-safety
到目前为止,我的应用程序一切正常,除了我不断收到一个错误:
type 'String' is not a subtype of type 'File'
我尝试了很多方法来尝试解决问题,但仍未解决任何问题。
我能理解问题出在哪里,但我尝试了无数次都无法解决它。
问题是我使用 ImagePicker gallery
传递一个 Image
,我将该图像数据作为 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)
似乎不是这样做:
image: carData['image']
你应该这样做:
image: File(carData['image'])
因为 carData['image']
是 String
,而不是 File
,并且 AddCar
需要 File
。
有时是因为使用 !空安全你应该 运行 喜欢 flutter 运行 --no-sound-null-safety