Home > Back-end >  treat CSV document to retrieve data separately obtained with file_picker package in flutter web
treat CSV document to retrieve data separately obtained with file_picker package in flutter web

Time:11-17

First I apologize for the English. I'm trying to format the data from a csv file obtained with the file_picker package in which I'm trying to separate the information to insert the information into a database.

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';
    }
  }

Could someone tell me how I can do this because that way the last section with line information ends up joining the first field of the next line.

GeneralRegistersModel (id: null, name: Hadila, email: [email protected], 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: [email protected], cpf: (16)96584-3214, idUser: null)
GeneralRegistersModel (id: null, name: [email protected], email: (16)96584-3213, phone: 154.368.954-92
Bruno, cpf: [email protected], idUser: null)
GeneralRegistersModel (id: null, name: 154.368.954-91
Vanesa, email: [email protected], 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: [email protected], cpf: (16)96584-3210, idUser: null)
GeneralRegistersModel (id: null, name: [email protected], email: (16)96584-3209, phone: 154.368.954-88
Daniel, cpf: [email protected], idUser: null)
GeneralRegistersModel (id: null, name: 154.368.954-87
Bianca, email: [email protected], 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: [email protected], cpf: (16)96584-3206, idUser: null)
GeneralRegistersModel (id: null, name: [email protected], email: (16)96584-3205, phone: 154.368.954-84
Fabricio, cpf: [email protected], idUser: null)
GeneralRegistersModel (id: null, name: 154.368.954-83
Claudia, email: [email protected], 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

And to make matters worse I'm getting an error that I don't know how to solve.

CodePudding user response:

you should first split the contacts bye whitespaces then split each contact bye semicolon

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],
    ));
}

NOTE: this will only work if all the CSVs loaded have this specific format

  • Related