将字符串路径转换为文件 FLUTTER

Convert String path to File FLUTTER

我有包含如下路径的列表

/storage/emulated/0/Whatsapp/Media/Whatsapp Images/IMG-20210623-WA0016.jpg

始终将其转换为文件 returns false。

 for (var i = 0; i < tempLocations.length; i++) {
      print(tempLocations.length);

      File newFile = File(tempLocations[0]);
      if (await newFile.exists()) {
        print("true");
      } else {
        print("false");
      }
    }

如何将其转换为文件?

您可以使用flutter_absolute_path获取准确路径

A flutter plugin that finds the absolute path of a file in iOS or Android devices.

无安全版本

flutter_absolute_path:
git:
url: https://github.com/kornperkus/flutter_absolute_path.git
     // Platform messages are asynchronous, so we initialize in an async method.
      Future<void> init() async {
        /// uri can be of android scheme content or file
        /// for iOS PHAsset identifier is supported as well
    
        List<Asset> assets = await selectImagesFromGallery();
        List<File> files = [];
        for (Asset asset in assets) {
// asset.identifier replace with your path
          final filePath =
              await FlutterAbsolutePath.getAbsolutePath(asset.identifier);
          files.add(File(filePath));
        }
    
        // If the widget was removed from the tree while the asynchronous platform
        // message was in flight, we want to discard the reply rather than calling
        // setState to update our non-existent appearance.
        if (!mounted) return;
    
        setState(() {
          _files = files;
        });
      }