flutter - 图像选择器 - 需要将 'List<XFile>' 转换为 'List<File>?'
flutter - Image picker - need to convert 'List<XFile>' to 'List<File>?'
我正在使用 image_picker 包来获取图像并在轮播中显示它们。
postNotifier(true).selectedPostImages != null
? Container(
height: 400,
width: 400,
child: CarouselSlider.builder(
options: CarouselOptions(height: 300),
itemCount: selectedImagesList!.length,
itemBuilder: (BuildContext context, index, realIndex) {
final selectedImage = selectedImagesList[index];
return buildImage(selectedImage, index);
}),
)
: const SizedBox(
height: 0,
),
我正在从这个 buildImage() 小部件中获取轮播生成器的图像
Widget buildImage(File selectedImage, int index) => Container(
margin: EdgeInsets.symmetric(horizontal: 12),
color: Colors.grey,
child: Image.file(selectedImage),
);
我的 PostNotifier class 在另一个文件中
class PostNotifier extends ChangeNotifier {
List<File>? _selectedPostImages;
List<File>? get selectedPostImages => _selectedPostImages;
Future<List?> pickPostImages() async {
final _imageList = await ImagePicker().pickMultiImage(imageQuality: 5);
if (_imageList != null) {
_selectedPostImages = _imageList; //I'm getting the error here.
notifyListeners();
}
}
flutter显示的错误是:
A value of type 'List' can't be assigned to a variable of type 'List?'.
Try changing the type of the variable, or casting the right-hand type to 'List?'.
在这种情况下,我尝试按照其他答案中的建议使用 flutter_absolute_path 来获取路径并将其设置为数组,但是当我在模拟器上 运行 时出现此错误
The plugin `flutter_absolute_path` uses a deprecated version of the Android embedding.
To avoid unexpected runtime failures, or future build failures, try to see if this plugin supports the Android V2 embedding. Otherwise, consider removing it since a future release of Flutter will remove these deprecated APIs.
If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.
Launching lib\main.dart on Android SDK built for x86 in debug mode...
lib\main.dart:1
/C:/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_absolute_path-1.0.6/lib/flutter_absolute_path.dart:11:23: Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher.
static Future<String?> getAbsolutePath(String uri) async {
^
Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:
- package:flutter_absolute_path
For solutions, see https://dart.dev/go/unsound-null-safety
: Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
lib/…/notifier/post.notifier.dart:101
File tempFile = File(filePath);
^
/C:/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_absolute_path-1.0.6/lib/flutter_absolute_path.dart:15:17: Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher.
final String? path = await _channel.invokeMethod('getAbsolutePath', params);
^
2
FAILURE: Build failed with an exception.
* Where:
Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1102
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 48s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)
并且该应用程序永远不会在模拟器上打开。
我该怎么办?我想实现这个多图像选择器。
XFile
是 image_picker
包对所选文件的包装器。因此,您从 await ImagePicker().pickMultiImage()
调用中获得 List<XFile>
,但尝试将其分配给需要 List<File>
的 _selectedPostImages
字段,这会产生类型不匹配错误。
因此,您可以:
- 重写
_selectedPostImages
以期望 List<XFile>
,如 List<XFile>? _selectedPostImages
。
- 将
_imageList
映射到 File
的列表,例如 _selectedPostImages = _imageList.map<File>((xfile) => File(xfile.path)).toList()
我正在使用 image_picker 包来获取图像并在轮播中显示它们。
postNotifier(true).selectedPostImages != null
? Container(
height: 400,
width: 400,
child: CarouselSlider.builder(
options: CarouselOptions(height: 300),
itemCount: selectedImagesList!.length,
itemBuilder: (BuildContext context, index, realIndex) {
final selectedImage = selectedImagesList[index];
return buildImage(selectedImage, index);
}),
)
: const SizedBox(
height: 0,
),
我正在从这个 buildImage() 小部件中获取轮播生成器的图像
Widget buildImage(File selectedImage, int index) => Container(
margin: EdgeInsets.symmetric(horizontal: 12),
color: Colors.grey,
child: Image.file(selectedImage),
);
我的 PostNotifier class 在另一个文件中
class PostNotifier extends ChangeNotifier {
List<File>? _selectedPostImages;
List<File>? get selectedPostImages => _selectedPostImages;
Future<List?> pickPostImages() async {
final _imageList = await ImagePicker().pickMultiImage(imageQuality: 5);
if (_imageList != null) {
_selectedPostImages = _imageList; //I'm getting the error here.
notifyListeners();
}
}
flutter显示的错误是:
A value of type 'List' can't be assigned to a variable of type 'List?'. Try changing the type of the variable, or casting the right-hand type to 'List?'.
在这种情况下,我尝试按照其他答案中的建议使用 flutter_absolute_path 来获取路径并将其设置为数组,但是当我在模拟器上 运行 时出现此错误
The plugin `flutter_absolute_path` uses a deprecated version of the Android embedding.
To avoid unexpected runtime failures, or future build failures, try to see if this plugin supports the Android V2 embedding. Otherwise, consider removing it since a future release of Flutter will remove these deprecated APIs.
If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.
Launching lib\main.dart on Android SDK built for x86 in debug mode...
lib\main.dart:1
/C:/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_absolute_path-1.0.6/lib/flutter_absolute_path.dart:11:23: Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher.
static Future<String?> getAbsolutePath(String uri) async {
^
Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:
- package:flutter_absolute_path
For solutions, see https://dart.dev/go/unsound-null-safety
: Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
lib/…/notifier/post.notifier.dart:101
File tempFile = File(filePath);
^
/C:/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_absolute_path-1.0.6/lib/flutter_absolute_path.dart:15:17: Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher.
final String? path = await _channel.invokeMethod('getAbsolutePath', params);
^
2
FAILURE: Build failed with an exception.
* Where:
Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1102
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 48s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)
并且该应用程序永远不会在模拟器上打开。
我该怎么办?我想实现这个多图像选择器。
XFile
是 image_picker
包对所选文件的包装器。因此,您从 await ImagePicker().pickMultiImage()
调用中获得 List<XFile>
,但尝试将其分配给需要 List<File>
的 _selectedPostImages
字段,这会产生类型不匹配错误。
因此,您可以:
- 重写
_selectedPostImages
以期望List<XFile>
,如List<XFile>? _selectedPostImages
。 - 将
_imageList
映射到File
的列表,例如_selectedPostImages = _imageList.map<File>((xfile) => File(xfile.path)).toList()