A package for managing data as a set of (tabular) records.
Each RecordSet is made up of Records which, in turn, are made up of
Fields.
If you're looking to parse delimited files into a RecordSet, check out the
betto_codec_csv package.
A range of approaches are available for loading data into a RecordSet. The
most basic approach is to just add a list of Records, each built from a list
of Objects, to the RecordSet. In this approach all data is treated as Strings.
For more complex data structures, a RecordSetHeader can be provided that
describes FieldSchema for each field. The FieldSchema provides a range of
information, including:
- The [FieldSchema.name] is the column name
- The [FieldSchema.type] is the data type
- [FieldSchema.defaultValue] is the default value to use if a
nullis provided - [FieldSchema.isNullable] indicates if the field can be
null - [FieldSchema.validators] can be used to validate the field value. A set of
pre-defined
Validators is provided in thebetto_schemapackage.
The RecordSetHeader can also be configured with a set of FieldParsers. These
provide a way to convert data from an incoming data type (usually a String) to
the intended data type. The FieldParser class provides a set of static
methods to help here, including [FieldParser.stringToInt] and
[FieldParser.stringToDateTime]. The FieldParsers class provides a base set of
FieldParsers for converting from a String to another type.
The approach is based on creating a RecordSet with a defined RecordSetHeader
and then adding Records to it. The RecordSet will then handle type changes
and validation of the incoming Records.
A RecordSet can then be rendered as HTML, JSON, or CSV using the Renderer
class. These are basic but can be readily extended.
Basic example demonstrating reading in comma-separated data and printing out
HTML (see example.dart in the example/ directory):
import 'package:betto_recordset/betto_recordset.dart';
import 'package:collection/collection.dart';
void main() {
const csvData = [
'id,name,rating',
'1,Black Forest Cake,5.0',
'2,Trifle,3.5',
'3,Ice cream cake,2.0',
'4,Tiramisu,4.5',
'5,Chocolate cake,3.0,Very Dry',
'6,Fruit Pizza,0.5',
];
final rs = RecordSet(
header: RecordSetHeader.fromStrings(
csvData[0].split(','),
),
);
for (final row in csvData.slice(1)) {
rs.newRecord(
row.split(','),
);
}
final html = Renderer(formatHtml).render(rs);
print(html);
}Will create HTML as follows:
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>rating</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Black Forest Cake</td>
<td>5.0</td>
</tr>
<tr>
<td>2</td>
<td>Trifle</td>
<td>3.5</td>
</tr>
<tr>
<td>3</td>
<td>Ice cream cake</td>
<td>2.0</td>
</tr>
<tr>
<td>4</td>
<td>Tiramisu</td>
<td>4.5</td>
</tr>
<tr>
<td>5</td>
<td>Chocolate cake</td>
<td>3.0</td>
</tr>
<tr>
<td>6</td>
<td>Fruit Pizza</td>
<td>0.5</td>
</tr>
</tbody>
</table>Which, when rendered appears as:
| id | name | rating |
|---|---|---|
| 1 | Black Forest Cake | 5.0 |
| 2 | Trifle | 3.5 |
| 3 | Ice cream cake | 2.0 |
| 4 | Tiramisu | 4.5 |
| 5 | Chocolate cake | 3.0 |
| 6 | Fruit Pizza | 0.5 |
You'll find example usage in the example/ directory and in test/. The
test/example_test.dart test file provides a more in-depth example of the
package.
This package is part of the Bettongia project.