首先,我为英国人道歉。我正在尝试格式化使用file_picker包获得的csv文件中的数据,在该包中,我试图将信息分开以将信息插入到数据库中。
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';
}
}
有人能告诉我如何做到这一点吗?因为这样,带行信息的最后一节就会加入下一行的第一个字段。
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
更糟糕的是,我得到了一个我不知道如何解决的错误。
发布于 2022-11-16 17:14:17
您应该先拆分联系人,然后再拆分每个联系人,再用分号
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都具有特定格式的时,这才能工作。
https://stackoverflow.com/questions/74462444
复制相似问题