简体   繁体   English

我需要从我用 file_picker flutter 选择的文件列表中删除一个文件

[英]i need to delete a file from my file list that i picked them with file_picker flutter

在此处输入图像描述 https://pub.dev/packages/file_picker i use this package this is part of my code,Like the image I uploaded ،i trying to delete just one file from the list just there is clearCachedFiles method in this package https://pub.dev/packages/file_picker i use this package this is part of my code,Like the image I uploaded ،i trying to delete just one file from the list just there is clearCachedFiles method in this package

 ListView.separated(
                       itemCount: _paths != null && _paths!.isNotEmpty ? _paths!.length : 1,
                         itemBuilder: (BuildContext context, int index) {
                         final bool isMultiPath = _paths != null && _paths!.isNotEmpty;
                              final String name = (isMultiPath ? _paths!.map((e) => e.name).toList()[index] : _fileName ?? '...');
                                               final path = kIsWeb ? null : _paths!.map((e) => e.path).toList()[index].toString();

                                               return ListTile(
                                                 leading: GestureDetector(
                                                   onTap: (){
                                                     setState(() {
                                                       _paths!.map((e) => e.path).toList().removeAt(index);
                                                       deleteFile(File(_paths!.map((e) => e.path).toList()[index].toString()));

                                                     });
                                                     if (kDebugMode) {
                                                       print('delete file ' + _paths!.map((e) => e.path).toList()[0].toString());
                                                     }
                                                   },
                                                     child: const Icon(Icons.highlight_remove_rounded,color: Colors.red,)),
                                                   trailing: Text(name),
                                               );
                                              },
                                              separatorBuilder: (BuildContext context, int index) => const Divider(),
                                        )),

and this method use for delete all files in this package
  void _clearCachedFiles() async {
    _resetState();
    try {
      bool? result = await FilePicker.platform.clearTemporaryFiles();
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          backgroundColor: result! ? Colors.green : Colors.red,
          content: Text((result ? 'Temporary files removed with success.' : 'Failed to clean temporary files')),
        ),
      );
    } on PlatformException catch (e) {
      _logException('Unsupported operation' + e.toString());
    } catch (e) {
      _logException(e.toString());
    } finally {
      setState(() => _isLoading = false);
    }
  }

Okay so to remove an item from the list you have to call List.removeAt() function and also has to pass the index of the item inside parenthesis like below:好的,要从列表中remove一个项目,您必须调用 List.removeAt List.removeAt() function 并且还必须在parenthesis传递项目的索引,如下所示:

leading: GestureDetector(
                                                   onTap: (){
                                                      _paths!.removeAt(index); //call this functions to remove item from your list
                                                     setState(() {
                                                       _paths!.map((e) => e.path).toList().removeAt(index);
                                                       deleteFile(File(_paths!.map((e) => e.path).toList()[index].toString()));

                                                     });
                                                     if (kDebugMode) {
                                                       print('delete file ' + _paths!.map((e) => e.path).toList()[0].toString());
                                                     }
                                                   },
                                                     child: const Icon(Icons.highlight_remove_rounded,color: Colors.red,)),
                                                   trailing: Text(name),
                                               );
                                              },
                                              separatorBuilder: (BuildContext context, int index) => const Divider(),
     

                               ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM