首先我為英語道歉。我正在嘗試從使用 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
更糟糕的是,我遇到了一個我不知道如何解決的錯誤。
uj5u.com熱心網友回復:
您應該首先將聯系人拆分為空格,然后將每個聯系人拆分為分號
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://www.uj5u.com/ruanti/535421.html
標籤:扑文件选择器.io
