@@ -130,6 +130,7 @@ class ClassBuilder extends _ClassBuilderBase {
130
130
field .type ,
131
131
'fields[${field .index }]' ,
132
132
nestedBuilders : field .nestedBuilders ,
133
+ nullable : field .isNullable ,
133
134
)}' );
134
135
}
135
136
@@ -141,6 +142,7 @@ class ClassBuilder extends _ClassBuilderBase {
141
142
DartType type,
142
143
String variable, {
143
144
bool nestedBuilders,
145
+ bool nullable = true ,
144
146
}) {
145
147
String builderConstructor;
146
148
String typeToBeCasted;
@@ -182,29 +184,40 @@ class ClassBuilder extends _ClassBuilderBase {
182
184
'($castedVariable )'
183
185
'${shouldBeBuilt ? '.build()' : '' }' ;
184
186
187
+ if (! nullable) {
188
+ return buildExpression;
189
+ }
190
+
185
191
return '$variable == null ? null : $buildExpression ' ;
186
192
}
187
193
188
194
@override
189
195
String cast (
190
196
DartType type,
191
- String variable, [
192
- bool nestedBuilders = false ,
193
- ]) {
197
+ String variable, {
198
+ bool nestedBuilders,
199
+ bool nullable = true ,
200
+ }) {
194
201
if (! isBuiltOrBuiltCollection (type) &&
195
202
! isBuilderOrCollectionBuilder (type)) {
196
203
// This value needs no special treatment.
197
204
return super .cast (type, variable);
198
205
}
199
206
200
- if ((isBuilt (type) && nestedBuilders) || isBuilder (type)) {
207
+ if ((isBuilt (type) && nestedBuilders == true ) || isBuilder (type)) {
201
208
// We need to call .toBuilder(), because variable is always an Built
202
209
// value, but we need an Builder value.
203
- return '($variable as ${_displayString (type )})?.toBuilder()' ;
210
+ final toBuilder = '${nullable ? '?' : '' }.toBuilder()' ;
211
+ return '($variable as ${_displayString (type )})$toBuilder ' ;
204
212
}
205
213
206
214
if (isBuiltCollection (type) || isCollectionBuilder (type)) {
207
- return _castBuiltCollection (type, variable, nestedBuilders ?? false );
215
+ return _castBuiltCollection (
216
+ type,
217
+ variable,
218
+ nestedBuilders: nestedBuilders,
219
+ nullable: nullable ?? true ,
220
+ );
208
221
}
209
222
210
223
// We just need to cast the value. This happens when the type is of a Built
0 commit comments