Flutter-windows dart.io 没有按预期工作,errno = 32

Flutter-windows dart.io not working as expected, errno = 32

当试图删除文件的父文件夹时,显然它 return OS Error: <message>, errno = 32:

例如,此代码永远不起作用:

File file = File("<path-to-file>");
file.parent.deleteSync();

这里是简单演示的完整代码:

import 'dart:io';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String? path;
  bool isAsync = false;
  bool recursive = false;

  final String filePath = "C:\flutter_demo\text.txt";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Checkbox(
                  value: isAsync,
                  onChanged: (value) {
                    setState(() {
                      isAsync = value!;
                    });
                  },
                ),
                const Text("use async"),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Checkbox(
                  value: recursive,
                  onChanged: (value) {
                    setState(() {
                      recursive = value!;
                    });
                  },
                ),
                const Text("recursive deletion"),
              ],
            ),
            TextButton(onPressed: isAsync ? deletDirectoryAsync : deletDirectorySync, child: Text("Delete file: '$filePath' parent folder")),
          ],
        ),
      ),
    );
  }

  void deletDirectoryAsync() async {
    Directory dir = File(filePath).parent;
    await dir.delete(recursive: recursive);
  }

  void deletDirectorySync() {
    Directory dir = File(filePath).parent;
    dir.deleteSync(recursive: recursive);
  }
}

拜托,你能帮忙吗?

目录有问题,实例化一个目录会打开一个句柄。 我通过将 Directory.current 更改为临时位置解决了这个问题:

//to avoid errno = 32
Directory.current = await getTemporaryDirectory();
FilePickerResult? result = await FilePicker.platform.pickFiles();
if(result!=null){
   String parent = 'get specific parent path from result';
   //do works 
   Directory(parent).deleteSync(recursive: true);
}