@@ -10,37 +10,37 @@ class CodeChunks {
10
10
ModelInfo model, List <String > imports, Pubspec ? pubspec) =>
11
11
"""
12
12
// GENERATED CODE - DO NOT MODIFY BY HAND
13
-
13
+
14
14
// ignore_for_file: camel_case_types
15
-
15
+
16
16
import 'dart:typed_data';
17
-
18
- import 'package:objectbox/flatbuffers /flat_buffers.dart' as fb;
17
+
18
+ import 'package:flat_buffers /flat_buffers.dart' as fb;
19
19
import 'package:objectbox/internal.dart'; // generated code can access "internal" functionality
20
20
import 'package:objectbox/objectbox.dart';${pubspec ?.obxFlutterImport }
21
-
21
+
22
22
import '${sorted (imports ).join ("';\n import '" )}';
23
-
23
+
24
24
export 'package:objectbox/objectbox.dart'; // so that callers only have to import this file
25
-
25
+
26
26
final _entities = <ModelEntity>[
27
27
${model .entities .map (createModelEntity ).join (',' )}
28
28
];
29
-
29
+
30
30
/// Open an ObjectBox store with the model declared in this file.
31
31
${openStore (model , pubspec )}
32
-
33
- /// ObjectBox model definition, pass it to [Store] - Store(getObjectBoxModel())
32
+
33
+ /// ObjectBox model definition, pass it to [Store] - Store(getObjectBoxModel())
34
34
ModelDefinition getObjectBoxModel() {
35
35
${defineModel (model )}
36
-
36
+
37
37
final bindings = <Type, EntityDefinition>{
38
38
${model .entities .mapIndexed ((i , entity ) => "${entity .name }: ${entityBinding (i , entity )}" ).join (",\n " )}
39
39
};
40
-
40
+
41
41
return ModelDefinition(model, bindings);
42
42
}
43
-
43
+
44
44
${model .entities .mapIndexed (_metaClass ).join ("\n " )}
45
45
""" ;
46
46
@@ -94,16 +94,16 @@ class CodeChunks {
94
94
static String createModelEntity (ModelEntity entity) {
95
95
return '''
96
96
ModelEntity(
97
- id: ${createIdUid (entity .id )},
98
- name: '${entity .name }',
99
- lastPropertyId: ${createIdUid (entity .lastPropertyId )},
100
- flags: ${entity .flags },
97
+ id: ${createIdUid (entity .id )},
98
+ name: '${entity .name }',
99
+ lastPropertyId: ${createIdUid (entity .lastPropertyId )},
100
+ flags: ${entity .flags },
101
101
properties: <ModelProperty>[
102
102
${entity .properties .map (createModelProperty ).join (',' )}
103
- ],
103
+ ],
104
104
relations: <ModelRelation>[
105
105
${entity .relations .map (createModelRelation ).join (',' )}
106
- ],
106
+ ],
107
107
backlinks: <ModelBacklink>[
108
108
${entity .backlinks .map (createModelBacklink ).join (',' )}
109
109
]
@@ -122,9 +122,9 @@ class CodeChunks {
122
122
}
123
123
return '''
124
124
ModelProperty(
125
- id: ${createIdUid (property .id )},
126
- name: '${property .name }',
127
- type: ${property .type },
125
+ id: ${createIdUid (property .id )},
126
+ name: '${property .name }',
127
+ type: ${property .type },
128
128
flags: ${property .flags }
129
129
$additionalArgs
130
130
)
@@ -134,8 +134,8 @@ class CodeChunks {
134
134
static String createModelRelation (ModelRelation relation) {
135
135
return '''
136
136
ModelRelation(
137
- id: ${createIdUid (relation .id )},
138
- name: '${relation .name }',
137
+ id: ${createIdUid (relation .id )},
138
+ name: '${relation .name }',
139
139
targetId: ${createIdUid (relation .targetId )}
140
140
)
141
141
''' ;
@@ -144,8 +144,8 @@ class CodeChunks {
144
144
static String createModelBacklink (ModelBacklink backlink) {
145
145
return '''
146
146
ModelBacklink(
147
- name: '${backlink .name }',
148
- srcEntity: '${backlink .srcEntity }',
147
+ name: '${backlink .name }',
148
+ srcEntity: '${backlink .srcEntity }',
149
149
srcField: '${backlink .srcField }'
150
150
)
151
151
''' ;
@@ -187,9 +187,9 @@ class CodeChunks {
187
187
// Such ID must already be set, i.e. it could not have been assigned.
188
188
return '''{
189
189
if (object.${propertyFieldName (entity .idProperty )} != id) {
190
- throw ArgumentError('Field ${entity .name }.${propertyFieldName (entity .idProperty )} is read-only '
190
+ throw ArgumentError('Field ${entity .name }.${propertyFieldName (entity .idProperty )} is read-only '
191
191
'(final or getter-only) and it was declared to be self-assigned. '
192
- 'However, the currently inserted object (.${propertyFieldName (entity .idProperty )}=\$ {object.${propertyFieldName (entity .idProperty )}}) '
192
+ 'However, the currently inserted object (.${propertyFieldName (entity .idProperty )}=\$ {object.${propertyFieldName (entity .idProperty )}}) '
193
193
"doesn't match the inserted ID (ID \$ id). "
194
194
'You must assign an ID before calling [box.put()].');
195
195
}
@@ -207,6 +207,10 @@ class CodeChunks {
207
207
return "''" ;
208
208
case 'List' :
209
209
return '[]' ;
210
+ case 'Int8List' :
211
+ return 'Int8List(0)' ;
212
+ case 'Uint8List' :
213
+ return 'Uint8List(0)' ;
210
214
default :
211
215
throw InvalidGenerationSourceError (
212
216
'Cannot figure out default value for field: ${p .fieldType } ${p .name }' );
@@ -331,24 +335,25 @@ class CodeChunks {
331
335
switch (p.type) {
332
336
case OBXPropertyType .ByteVector :
333
337
if (['Int8List' , 'Uint8List' ].contains (p.fieldType)) {
334
- // No need for the eager reader here. We need to call fromList()
335
- // constructor anyway - there's no Int8List.generate() factory.
336
- fbReader = 'fb.ListReader<int>(fb.Int8Reader())' ;
337
- if (p.fieldIsNullable) {
338
- preLines.add ('final $valueVar = ${readFieldOrNull ()};' );
339
- return '$valueVar == null ? null : ${p .fieldType }.fromList($valueVar )' ;
340
- } else {
341
- return '${p .fieldType }.fromList(${readFieldNonNull ('[]' )})' ;
342
- }
338
+ fbReader = 'fb.${p .fieldType }Reader(lazy: false)' ;
339
+ return '${readField ()} as ${p .fieldType }${p .fieldIsNullable && p .entity !.nullSafetyEnabled ? "?" : "" }' ;
343
340
} else {
344
341
fbReader = 'fb.ListReader<int>(fb.Int8Reader(), lazy: false)' ;
345
342
}
346
343
break ;
347
344
case OBXPropertyType .Relation :
348
345
fbReader = 'fb.${_propertyFlatBuffersType [p .type ]}Reader()' ;
349
346
return readFieldNonNull ('0' );
347
+ case OBXPropertyType .String :
348
+ // TODO `asciiOptimization: true` is for keeping the same behavior as the previous FB fork.
349
+ // Check if it still makes sense with the latest Dart/Flutter.
350
+ fbReader = 'fb.StringReader(asciiOptimization: true)' ;
351
+ break ;
350
352
case OBXPropertyType .StringVector :
351
- fbReader = 'fb.ListReader<String>(fb.StringReader(), lazy: false)' ;
353
+ // TODO `asciiOptimization: true` is for keeping the same behavior as the previous FB fork.
354
+ // Check if it still makes sense with the latest Dart/Flutter.
355
+ fbReader =
356
+ 'fb.ListReader<String>(fb.StringReader(asciiOptimization: true), lazy: false)' ;
352
357
break ;
353
358
default :
354
359
fbReader = 'fb.${_propertyFlatBuffersType [p .type ]}Reader()' ;
@@ -607,7 +612,7 @@ class CodeChunks {
607
612
}
608
613
609
614
return '''
610
- /// [${entity .name }] entity fields to define ObjectBox queries.
615
+ /// [${entity .name }] entity fields to define ObjectBox queries.
611
616
class ${entity .name }_ {${fields .join ()}}
612
617
''' ;
613
618
}
0 commit comments