我需要从我的文件列表中删除一个文件,我用file_picker flutter拾取了它们

qgelzfjb  于 2023-05-08  发布在  Flutter
关注(0)|答案(1)|浏览(116)


https://pub.dev/packages/file_picker我使用这个包这是我的代码的一部分,像我上传的图像,我试图从列表中删除一个文件只是有clearCachedFiles方法在这个包

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);
    }
  }
nwwlzxa7

nwwlzxa71#

好了,要从列表中remove一个项目,你必须调用List.removeAt()函数,还必须像下面这样传递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(),
     

                               ))

相关问题