Specify (at least a major) version when adding this project as dependency. Whenever the API has incompatible changes the major version changes!
parseNumbershas been renamed to
FirstOccurenceSettingsDetectorhas been renamed to
A dart csv to list codec / converter.
If you have a
String of all rows with RFC conform separators and delimiters,
simply convert them with:
List<List<dynamic>> rowsAsListOfValues = const CsvToListConverter().convert(yourString);
To convert to a Csv string your values must be in a
representing a List of Rows where every Row is a List of values.
You can then convert with:
String csv = const ListToCsvConverter().convert(yourListOfLists);
The default (RFC conform) configuration is:
See below if you need other settings, or want to autodetect them.
This converter is implemented as codec and may be used as transformer for streams:
final csvCodec = new CsvCodec(); final stream = new Stream.fromIterable([['a', 'b'], [1, 2]]); final csvRowStream = stream.transform(csvCodec.encoder);
Or the decoder side:
final input = new File('a/csv/file.txt').openRead(); final fields = await input.transform(UTF8.decoder).transform(csvCodec.decoder).toList();
The converter is highly customizable and even allows multiple characters as delimiters or separators.
Every csv row is converted to a list of values. Unquoted strings looking like
numbers (integers and doubles) are by default converted to
The input must be a
Lists. Every inner list is converted to one
output csv row. The string representation of values is obtained by calling
This converter follows the rules of rfc4180.
This means that text fields containing any delimiter or an eol are quoted.
The default configuration is:
This parser will accept eol and text-delimiters inside unquoted text and not throw an error.
In addition this converter supports multiple characters for all delimiters
and eol. Also the start text delimiter and end text delimiter may be
different. This means the following text can be parsed:
And (if configured correctly) will return
['abc«d', 'xy»z', 123]
If the default values are fine, simply instantiate
final res = const ListToCsvConverter().convert([[',b', 3.1, 42], ['n\n']]); assert(res == '",b",3.1,42\r\n"n\n"');
There are 2 interesting things to note:
'\n'is also quoted. The appearance of only one character is enough for the string to be quoted.
The converter takes the following configurations either in the constructor or
fieldDelimiter: the separator between fields. By default
','but another common value is
textDelimiter: the quotation string. By default
textEndDelimiter: the end quotation string. By default equals
textDelimiter. The string used to end a quoted string.
eol: The new line string. By default
'\r\n'. Another common value:
All configuration values may be multiple characters!:
const conv = const ListToCsvConverter(fieldDelimiter: '|*|', textDelimiter: '<<', textEndDelimiter: '>>', eol: '**\n'); final res = conv.convert([['a','>'], ['<<', '>>'], [1, 2]]); assert(res == 'a|*|<<>>>**\n<<<<>>|*|<<>>>>>>**\n1|*|2'); final res2 = const ListToCsvConverter() .convert([['a','>'], ['<<', '>>'], [1, 2]], fieldDelimiter: '|*|', textDelimiter: '<<', textEndDelimiter: '>>', eol: '**\n'); assert(res == res2);
'<<'is quoted as well, but because it is "only" a start text delimiter it is not doubled. (See rule 7. below).
'>>'is quoted. Only the end-quote string is doubled!
If the default values are fine, simply instantiate
final res = const CsvToListConverter().convert('",b",3.1,42\r\n"n\n"'); assert(res.toString() == [[',b', 3.1, 42], ['n\n']].toString());
Again please note that depending on the input not all rows have the same number of values.
CsvToListConverter takes the same arguments as the
shouldParseNumbers: by default true. If you want the output to be
set this to false.
allowInvalid: by default true. The converter will by default never throw
an exception. Even if
textDelimiter,... don't make sense
or the csv-String is invalid. This may for instance happen if the csv-String
ends with a quoted String without the end-quote (
csvSettingsDetector: must be an object which extends from
CsvSettingsDetector. There implementation simply selects the first occurrence
of a list of possible values as value.
var d = new FirstOccurrenceSettingsDetector(eols: ['\r\n', '\n'], textDelimiters: ['"', "'"]); new CsvToListConverter(csvSettingsDetector: d);
In this case
eol will either be
'\n' depending on which of
those 2 comes first in the csv string. Note that the
FirstOccurrenceSettingsDetector doesn't parse the csv string! For instance
if eol should be
'\r\n' but there is a field with a correctly quoted
in the first row,
'\n' is used instead.
If you csv String contains a (simple) header row, or all eols are equal this is good enough.
Feel free to submit something more intelligent.
To check your configuration values there is
verifyCurrentSettings. Both return an empty list if all settings are valid,
or a list of errors. If the optional
throwError is true an error is thrown in
case the settings are invalid.
All settings must be set, i.e. not be null, and delimiters, separators and eols must be distinguishable, i.e. they may not be the start of another settings.
Ad rule 3: removed as it is not relevant for this converter.
Each record is located on a separate line, delimited by a line break (CRLF). For example: aaa,bbb,ccc CRLF zzz,yyy,xxx CRLF
The last record in the file may or may not have an ending line break.
Within the header and each record, there may be one or more fields, separated by commas. Each line should contain the same number of fields throughout the file. Spaces are considered part of a field and should not be ignored. The last field in the record must not be followed by a comma. For example:
Each field may or may not be enclosed in double quotes (however some programs, such as Microsoft Excel, do not use double quotes at all). If fields are not enclosed with double quotes, then double quotes may not appear inside the fields. For example:
"aaa","bbb","ccc" CRLF zzz,yyy,xxx
Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:
"aaa","b CRLF bb","ccc" CRLF zzz,yyy,xxx
If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:
Add this to your package's pubspec.yaml file:
dependencies: csv: ^4.0.0
You can install packages from the command line:
$ pub get
$ flutter packages get
Alternatively, your editor might support
pub get or
flutter packages get.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
|4.0.0||Jul 3, 2018|
|3.3.0||Jul 3, 2018|
|3.2.0||Jun 26, 2018|
|3.1.2||Feb 15, 2017|
|3.1.1||Jun 15, 2016|
|3.0.1||Feb 1, 2016|
|2.0.1||Oct 5, 2014|
|2.0.0||Oct 5, 2014|
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
We analyzed this package on Aug 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, web, other
No platform restriction found in primary library
Changelog entries help clients to follow the progress in your code.
Maintain an example.
Create a short demo in the
example/ directory to show how to use this package. Common file name patterns include:
example.dart or you could also use
|Dart SDK||>=2.0.0-dev.0.0 <2.1.0|