dart 如何在列表中添加一些图像

nfzehxib  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(110)

我只是一个初学者flutter,我想从文件夹中读取一些图像,并把它们放在一个列表中,但我不能读他们无论我做什么,在控制台我得到一个类型的错误称为_

  1. Future<List<File>> getImagesFromFolder(String folderPath) async {
  2. Directory directory = Directory(folderPath);
  3. List<File> imageFiles = [];
  4. int i = 0;
  5. if (await directory.exists()) {
  6. List<FileSystemEntity> entities = directory.listSync();
  7. for (var entity in entities) {
  8. if (entity is File) {
  9. imageFiles.add(entity);
  10. }
  11. }
  12. }
  13. return imageFiles;
  14. }
  15. class ImageListFromFolder extends StatefulWidget {
  16. @override
  17. _ImageListFromFolderState createState() => _ImageListFromFolderState();
  18. }
  19. class _ImageListFromFolderState extends State<ImageListFromFolder> {
  20. List<File> _imageFiles = [];
  21. @override
  22. void initState() {
  23. super.initState();
  24. _loadImages();
  25. }
  26. Future<void> _loadImages() async {
  27. String folderPath = '(here i insert the path using '/' but is still not reading )';
  28. List<File> images = await getImagesFromFolder(folderPath);
  29. setState(() {
  30. _imageFiles = images;
  31. });
  32. }
lbsnaicq

lbsnaicq1#

代码可能遇到问题,因为无法将/字符识别为图像文件所在的有效目录分隔符。此外,我还改进了代码,以提高效率和可读性,供将来使用。

  1. import 'dart:io';
  2. import 'package:path_provider/path_provider.dart';
  3. class FolderFileList {
  4. final String folderPath;
  5. // Constructor that takes the folder path as an argument
  6. FolderFileList(this.folderPath);
  7. /// Returns a list of file directories for supported image files in the specified folder.
  8. /// If the folder does not exist or any error occurs, it will return an empty list.
  9. Future<List<String>> getFilesInFolder() async {
  10. // List of supported file extensions
  11. final List<String> supportedExtensions = ['png', 'jpg', 'tiff'];
  12. final List<String> fileDirectories = [];
  13. try {
  14. // Get the directory object for the specified folder path
  15. final Directory folderDirectory = Directory(folderPath);
  16. if (await folderDirectory.exists()) {
  17. // List all files in the folder
  18. final List<FileSystemEntity> files = folderDirectory.listSync();
  19. for (final file in files) {
  20. if (file is File) {
  21. // Get the file extension and convert it to lowercase
  22. final String extension = file.path.split('.').last.toLowerCase();
  23. // Check if the file extension is supported
  24. if (supportedExtensions.contains(extension)) {
  25. // Add the file directory to the list
  26. fileDirectories.add(file.path);
  27. }
  28. }
  29. }
  30. }
  31. } catch (error) {
  32. // Handle any errors that occur during file retrieval
  33. print('Error: An error occurred while retrieving files. $error');
  34. }
  35. // Returns the list of file directories for supported image files
  36. return fileDirectories;
  37. }
  38. class ImageReader {
  39. final String imagePath;
  40. // Constructor that takes the file path as an argument
  41. ImageReader(this.imagePath);
  42. /// Reads the image from the specified file path and returns it as an [Image] object.
  43. /// Returns null if any error occurs during image reading.
  44. Future<Image?> readImage() async {
  45. try {
  46. final File imageFile = File(imagePath);
  47. if (await imageFile.exists()) {
  48. final List<int> imageBytes = await imageFile.readAsBytes();
  49. final Image? decodedImage = decodeImage(Uint8List.fromList(imageBytes));
  50. return decodedImage;
  51. }
  52. } catch (error) {
  53. // Handle any errors that occur during image reading
  54. print('Error: An error occurred while reading the image. $error');
  55. }
  56. return null;
  57. }
  58. }
展开查看全部

相关问题