treatment of null
Hi,
Would you consider adding another parameter to ListToCsvConverter that will customize how nulls get treated? Currently, for [1, null, 3] you get 1,null,3. That is inconvenient if you import the output into a spreadsheet program as the nulls will stick out. I would prefer the ability to convert the nulls to empty strings or maybe something else. Of course, I can preprocess my data, but that requires another pass.
What do you think?
Thanks, Tony
sounds reasonable. I will add this option.
If you use the transform version this could currently (in a complicated way) be done, without 2 passes:
test('Works as transformer nulls replaced', () {
var eol = commaDoubleQuotListToCsvConverter.eol;
var result = 'row1,1,value' + eol + 'row2,2,' + eol;
var originalList = [
['row1', 1, 'value'],
['row2', 2, null]
];
var nullReplacedIterable = originalList.map((row) =>
row.map((col) => col == null ? '' : col).toList(growable: false));
var s = Stream.fromIterable(nullReplacedIterable);
Future<String> f_csv = s.transform(commaDoubleQuotListToCsvConverter).join();
expect(f_csv, completion(result));
});
hi @close2 was the option to treat null as empty string added, went through the code and docs and couldn't find it. If not please do consider it when you have some time, as it's creating two issues, first putting null as a value when we write files and second it increases the file size
Sorry not yet...
for me, is not just an improvement but a real issue.
Because when I have this line in a CSV file (I do not change this format!) : 18/09/2023;3435frgt;The is a basic label string here;12;
This package count 5 fields and this is very good think because the last field is an empty (but important!) field. BUT the return of this field can't work with this
because :
print(row[4]); // return nothing into console
print(row[4] == ''); // false
print(row[4] == ' '); // false
print(row[4] == null); // false
print(row[4].toString().isEmpty); // false
The empty fields in CSV is understand by the package but nothing process for convert this correctly for working after when convert CSV --> to List
I'm very confused because I can't change my CSV format source.
Do you have news about this issue/improvement please ?
Thank you for your help and your works already done in this package.
@darkomenx I am currently working on this feature and noticed that your problem doesn't really match the initial bug report.
I think, that the library is finding some value in your case. The current implementation should convert an empty field to an empty string (row[4] == '' should be true).
My first guess is, that your EOLs are not configured correctly.
Please convert the string to bytes and print them:
print(row[4].length);
List<int> list = utf8.encode(row[4]);
print(list);
Added with release 5.1.0
(convertNullTo and convertEmptyTo)