【问题标题】:adding record from one List to another List将记录从一个列表添加到另一个列表
【发布时间】:2021-07-16 19:51:22
【问题描述】:

在清单一中,我得到了一些物品。每次这些项目都在变化。有时,我可以在列表中获得不止一条记录。 在第二个列表中,我想存储列表一的所有数据。这样,我就可以显示列表二的所有项目了。

为了更清楚。

列表一 = "/temp/file1.jpeg"

列表二 = "/temp/file1.jpeg"

列表一 = "/temp/file2.jpeg"

列表二 = "/temp/file1.jpeg,/temp/file2.jpeg"

我试过了

void _openDocumentFileExplorer({fileType: FileType.custom}) async {
    setState(() => _loadingPath = true);

    try{
      _paths = (await FilePicker.platform.pickFiles(
          type: fileType,
          allowMultiple: true,//_multiPick,
          allowedExtensions: ['pdf']))?.files;
    } on PlatformException catch (e) {
      print("Unsupported operation" + e.toString());
    } catch (ex) {
      print('$ex');
    }

    if (!mounted) return;

    setState(() {
      _loadingPath = false;
      _fileName = _paths != null ?
      _paths!.map((e) => e.name).toString() : '...';
    });
  }

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 = _paths!
                                  .map((e) => e.name)
                                  .toList()[index];

                              //filesGB store the full path + the file name
                              final filesGB = _paths!
                                  .map((e) => e.path)
                                  .toList()[index]
                                  .toString();

                              print (filesGB);

                              _paths?.addAll(allFiles!.map((e) ));
                              allFiles.addAll(filesGB.toList());
                              allFiles.addAll(filesGB);
                             // allFilesV2.addAll(filesGB);

但它不起作用。我收到此错误消息。 "参数类型'String'不能赋值给参数类型'Iterable'"

请问您有什么建议吗?

【问题讨论】:

    标签: list flutter


    【解决方案1】:

    我认为您可以使用 SPREAD OPERATOR (...) 使用三点将一个数组合并到另一个数组中。

    例如:

    List list1= ["/temp/file1.jpeg"];
    
    List list2 = [];
    

    一段时间后

     list1 = ["/temp/file2.jpeg"];
    

    所以每当你的清单发生变化时

    list2 = [...list2,...list1];
    
    print(list2);
    

    输出:["/temp/file1.jpeg","/temp/file2.jpeg"]

    我认为这会有所帮助。

    【讨论】:

    • 谢谢。我不知道该怎么做。我会尝试在互联网上找到如何做到这一点。在我之前的测试中,我收到此错误消息“参数类型 'String' 无法分配给参数类型 'Iterable'”
    • 您的列表一的数据类型是什么?此错误是因为您正在传播一个不可能的字符串变量。您可以展开列表,但不能展开字符串。
    • 列出 filesGB =[];列表 allFiles = []; _paths = (await FilePicker.platform.pickFiles(type: FileType.image, allowMultiple: true,//_multiPick, ))?.files; } on PlatformException catch (e) { print("Unsupported operation" + e.toString()); } catch (ex) { print('$ex');} if (!mounted) return; setState(() { _loadingPath = false; _fileName = _paths != null ? _paths!.map((e) => e.name).toString() : '...'; });
    • 如果你想存储文件列表,那么 filesGB = _paths.map((e) => File(e)).toList();每当 filesGB 更改时,请执行 allFiles = [...allFiles,...filesGB];如果你想用逗号连接 allFiles 的所有元素,那么 String allPaths = allFiles.join(",");如果还有其他问题,请解释得更详细。
    • 谢谢。我已经尝试过,但出现错误无法将参数类型“PlatformFile”分配给参数类型“String”文件GB = _paths!.map((e) => File(e)).toList();问题似乎是((e)我也收到了一个错误,Image.file(File(filesGB),参数类型'List'不能分配给参数类型'String'。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2021-08-18
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2022-06-13
    • 2017-03-02
    相关资源
    最近更新 更多