首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在颤振网络中处理CSV文档分别检索用file_picker包获取的数据

在颤振网络中处理CSV文档分别检索用file_picker包获取的数据
EN

Stack Overflow用户
提问于 2022-11-16 14:42:45
回答 1查看 18关注 0票数 0

首先,我为英国人道歉。我正在尝试格式化使用file_picker包获得的csv文件中的数据,在该包中,我试图将信息分开以将信息插入到数据库中。

代码语言:javascript
运行
复制
Future<String> pickFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      dialogTitle: 'Selecione o arquivo',
      allowMultiple: true,
      type: FileType.custom,
      allowedExtensions: ['csv'],
    );

    if (result != null && result.files.isNotEmpty) {
      var fileBytes = result.files.first.bytes;
      var formater = String.fromCharCodes(fileBytes!);

      //print(formater);

      List<String> listCsv = formater.split(";");

      String nome = '';
      String telefone = '';
      String email = '';
      String cpf = '';

      for (int i = 0; i < listCsv.length; i += 4) {
        nome = listCsv[i];
        telefone = listCsv[i + 1];
        email = listCsv[i + 2];
        cpf = listCsv[i + 3];

        print(RegistersModel(name: nome, phone: telefone, cpf: cpf, email: email));
      }

      //final path = result.files.single.path;
      //print(path);

      return csv = formater;
    } else {
      return 'O documento não foi retornado';
    }
  }

有人能告诉我如何做到这一点吗?因为这样,带行信息的最后一节就会加入下一行的第一个字段。

代码语言:javascript
运行
复制
GeneralRegistersModel (id: null, name: Hadila, email: hadila.teste@gmail.com, phone: (16)96584-3216, cpf: 154.368.954-94
Ana, idUser: null)
GeneralRegistersModel (id: null, name: (16)96584-3215, email: 154.368.954-93
Amanda, phone: ana.teste@gmail.com, cpf: (16)96584-3214, idUser: null)
GeneralRegistersModel (id: null, name: amanda.teste@gmail.com, email: (16)96584-3213, phone: 154.368.954-92
Bruno, cpf: bruno.teste@gmail.com, idUser: null)
GeneralRegistersModel (id: null, name: 154.368.954-91
Vanesa, email: vanessa.teste@gmail.com, phone: (16)96584-3212, cpf: 154.368.954-90
Jonas, idUser: null)
GeneralRegistersModel (id: null, name: (16)96584-3211, email: 154.368.954-89
Murilo, phone: jonas.teste@gmail.com, cpf: (16)96584-3210, idUser: null)
GeneralRegistersModel (id: null, name: murilo.teste@gmail.com, email: (16)96584-3209, phone: 154.368.954-88
Daniel, cpf: daniel.teste@gmail.com, idUser: null)
GeneralRegistersModel (id: null, name: 154.368.954-87
Bianca, email: bianca.teste@gmail.com, phone: (16)96584-3208, cpf: 154.368.954-86
Pamela, idUser: null)
GeneralRegistersModel (id: null, name: (16)96584-3207, email: 154.368.954-85
Roberto, phone: pamela.teste@gmail.com, cpf: (16)96584-3206, idUser: null)
GeneralRegistersModel (id: null, name: roberto.teste@gmail.com, email: (16)96584-3205, phone: 154.368.954-84
Fabricio, cpf: vfabricio.teste@gmail.com, idUser: null)
GeneralRegistersModel (id: null, name: 154.368.954-83
Claudia, email: claudia.teste@gmail.com, phone: (16)96584-3204, cpf: 154.368.954-82
Pablo, idUser: null)
Error: RangeError (index): Index out of range: index should be less than 43: 43
    at Object.throw_ [as throw] (http://localhost:55593/dart_sdk.js:5080:11)
    at [dartx._get] (http://localhost:55593/dart_sdk.js:17547:21)
    at general_registers_controller.GeneralRegistersController.new.pickFile (http://localhost:55593/packages/project_002_italian_citizenship/src/controllers/general_registers_controller.dart.lib.js:169:33)
    at pickFile.next (<anonymous>)
    at http://localhost:55593/dart_sdk.js:40641:33
    at _RootZone.runUnary (http://localhost:55593/dart_sdk.js:40511:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:55593/dart_sdk.js:35438:29)
    at handleValueCallback (http://localhost:55593/dart_sdk.js:35999:49)
    at _Future._propagateToListeners (http://localhost:55593/dart_sdk.js:36037:17)
    at [_completeWithValue] (http://localhost:55593/dart_sdk.js:35872:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:55593/dart_sdk.js:35906:35)
    at Object._microtaskLoop (http://localhost:55593/dart_sdk.js:40778:13)
    at _startMicrotaskLoop (http://localhost:55593/dart_sdk.js:40784:13)
    at http://localhost:55593/dart_sdk.js:36261:9

更糟糕的是,我得到了一个我不知道如何解决的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-16 17:14:17

您应该先拆分联系人,然后再拆分每个联系人,再用分号

代码语言:javascript
运行
复制
final formater = String.fromCharCodes(fileBytes!);
final contacts = formater.split('\r'); // Carriage Return ascii code 13
for (int i = 0; i < contacts.length - 1; i++) {
    final contactData = contacts[i].split(';');
    print(RegistersModel(
        name: contactData[0],
        phone: contactData[1],
        email: contactData[2],
        cpf: contactData[3],
    ));
}

注意:只有当所有加载的CSV都具有特定格式的时,这才能工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74462444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档