@@ -86,7 +86,7 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
86
86
indent.writeln (
87
87
'// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import' ,
88
88
);
89
- indent.addln ( '' );
89
+ indent.newln ( );
90
90
}
91
91
92
92
@override
@@ -96,7 +96,7 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
96
96
indent.writeln (
97
97
"import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;" ,
98
98
);
99
- indent.addln ( '' );
99
+ indent.newln ( );
100
100
indent.writeln (
101
101
"import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;" );
102
102
indent.writeln ("import 'package:flutter/services.dart';" );
@@ -105,11 +105,11 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
105
105
@override
106
106
void writeEnum (
107
107
DartOptions generatorOptions, Root root, Indent indent, Enum anEnum) {
108
- indent.writeln ( '' );
108
+ indent.newln ( );
109
109
addDocumentationComments (
110
110
indent, anEnum.documentationComments, _docCommentSpec);
111
111
indent.write ('enum ${anEnum .name } ' );
112
- indent.scoped ('{' , '}' , () {
112
+ indent.addScoped ('{' , '}' , () {
113
113
for (final EnumMember member in anEnum.members) {
114
114
addDocumentationComments (
115
115
indent, member.documentationComments, _docCommentSpec);
@@ -126,33 +126,33 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
126
126
final Set <String > customEnumNames =
127
127
root.enums.map ((Enum x) => x.name).toSet ();
128
128
129
- indent.writeln ( '' );
129
+ indent.newln ( );
130
130
addDocumentationComments (
131
131
indent, klass.documentationComments, _docCommentSpec);
132
132
133
133
indent.write ('class ${klass .name } ' );
134
- indent.scoped ('{' , '}' , () {
134
+ indent.addScoped ('{' , '}' , () {
135
135
_writeConstructor (indent, klass);
136
- indent.addln ( '' );
136
+ indent.newln ( );
137
137
for (final NamedType field in getFieldsInSerializationOrder (klass)) {
138
138
addDocumentationComments (
139
139
indent, field.documentationComments, _docCommentSpec);
140
140
141
141
final String datatype = _addGenericTypesNullable (field.type);
142
142
indent.writeln ('$datatype ${field .name };' );
143
- indent.writeln ( '' );
143
+ indent.newln ( );
144
144
}
145
145
writeClassEncode (generatorOptions, root, indent, klass, customClassNames,
146
146
customEnumNames);
147
- indent.writeln ( '' );
147
+ indent.newln ( );
148
148
writeClassDecode (generatorOptions, root, indent, klass, customClassNames,
149
149
customEnumNames);
150
150
});
151
151
}
152
152
153
153
void _writeConstructor (Indent indent, Class klass) {
154
154
indent.write (klass.name);
155
- indent.scoped ('({' , '});' , () {
155
+ indent.addScoped ('({' , '});' , () {
156
156
for (final NamedType field in getFieldsInSerializationOrder (klass)) {
157
157
final String required = field.type.isNullable ? '' : 'required ' ;
158
158
indent.writeln ('${required }this.${field .name },' );
@@ -170,11 +170,11 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
170
170
Set <String > customEnumNames,
171
171
) {
172
172
indent.write ('Object encode() ' );
173
- indent.scoped ('{' , '}' , () {
173
+ indent.addScoped ('{' , '}' , () {
174
174
indent.write (
175
175
'return <Object?>' ,
176
176
);
177
- indent.scoped ('[' , '];' , () {
177
+ indent.addScoped ('[' , '];' , () {
178
178
for (final NamedType field in getFieldsInSerializationOrder (klass)) {
179
179
final String conditional = field.type.isNullable ? '?' : '' ;
180
180
if (customClassNames.contains (field.type.baseName)) {
@@ -207,27 +207,25 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
207
207
if (customClassNames.contains (field.type.baseName)) {
208
208
final String nonNullValue =
209
209
'${field .type .baseName }.decode($resultAt ! as List<Object?>)' ;
210
- indent.format (
211
- field.type.isNullable
212
- ? '''
210
+ if (field.type.isNullable) {
211
+ indent.format ('''
213
212
$resultAt != null
214
213
\t\t ? $nonNullValue
215
- \t\t : null'''
216
- : nonNullValue,
217
- leadingSpace : false ,
218
- trailingNewline : false );
214
+ \t\t : null''', leadingSpace : false , trailingNewline : false );
215
+ } else {
216
+ indent. add (nonNullValue);
217
+ }
219
218
} else if (customEnumNames.contains (field.type.baseName)) {
220
219
final String nonNullValue =
221
220
'${field .type .baseName }.values[$resultAt ! as int]' ;
222
- indent.format (
223
- field.type.isNullable
224
- ? '''
221
+ if (field.type.isNullable) {
222
+ indent.format ('''
225
223
$resultAt != null
226
224
\t\t ? $nonNullValue
227
- \t\t : null'''
228
- : nonNullValue,
229
- leadingSpace : false ,
230
- trailingNewline : false );
225
+ \t\t : null''', leadingSpace : false , trailingNewline : false );
226
+ } else {
227
+ indent. add (nonNullValue);
228
+ }
231
229
} else if (field.type.typeArguments.isNotEmpty) {
232
230
final String genericType = _makeGenericTypeArguments (field.type);
233
231
final String castCall = _makeGenericCastCall (field.type);
@@ -252,10 +250,10 @@ $resultAt != null
252
250
indent.write (
253
251
'static ${klass .name } decode(Object result) ' ,
254
252
);
255
- indent.scoped ('{' , '}' , () {
253
+ indent.addScoped ('{' , '}' , () {
256
254
indent.writeln ('result as List<Object?>;' );
257
255
indent.write ('return ${klass .name }' );
258
- indent.scoped ('(' , ');' , () {
256
+ indent.addScoped ('(' , ');' , () {
259
257
enumerate (getFieldsInSerializationOrder (klass),
260
258
(int index, final NamedType field) {
261
259
indent.write ('${field .name }: ' );
@@ -292,15 +290,15 @@ $resultAt != null
292
290
codecName = _getCodecName (api);
293
291
_writeCodec (indent, codecName, api, root);
294
292
}
295
- indent.addln ( '' );
293
+ indent.newln ( );
296
294
addDocumentationComments (
297
295
indent, api.documentationComments, _docCommentSpec);
298
296
299
297
indent.write ('abstract class ${api .name } ' );
300
- indent.scoped ('{' , '}' , () {
298
+ indent.addScoped ('{' , '}' , () {
301
299
indent
302
300
.writeln ('static const MessageCodec<Object?> codec = $codecName ();' );
303
- indent.addln ( '' );
301
+ indent.newln ( );
304
302
for (final Method func in api.methods) {
305
303
addDocumentationComments (
306
304
indent, func.documentationComments, _docCommentSpec);
@@ -314,14 +312,14 @@ $resultAt != null
314
312
_getArgumentName,
315
313
);
316
314
indent.writeln ('$returnType ${func .name }($argSignature );' );
317
- indent.writeln ( '' );
315
+ indent.newln ( );
318
316
}
319
317
indent.write (
320
318
'static void setup(${api .name }? api, {BinaryMessenger? binaryMessenger}) ' );
321
- indent.scoped ('{' , '}' , () {
319
+ indent.addScoped ('{' , '}' , () {
322
320
for (final Method func in api.methods) {
323
321
indent.write ('' );
324
- indent.scoped ('{' , '}' , () {
322
+ indent.addScoped ('{' , '}' , () {
325
323
indent.writeln (
326
324
'final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(' ,
327
325
);
@@ -337,15 +335,15 @@ $resultAt != null
337
335
final String messageHandlerSetter =
338
336
isMockHandler ? 'setMockMessageHandler' : 'setMessageHandler' ;
339
337
indent.write ('if (api == null) ' );
340
- indent.scoped ('{' , '}' , () {
338
+ indent.addScoped ('{' , '}' , () {
341
339
indent.writeln ('channel.$messageHandlerSetter (null);' );
342
340
}, addTrailingNewline: false );
343
341
indent.add (' else ' );
344
- indent.scoped ('{' , '}' , () {
342
+ indent.addScoped ('{' , '}' , () {
345
343
indent.write (
346
344
'channel.$messageHandlerSetter ((Object? message) async ' ,
347
345
);
348
- indent.scoped ('{' , '});' , () {
346
+ indent.addScoped ('{' , '});' , () {
349
347
final String returnType =
350
348
_addGenericTypesNullable (func.returnType);
351
349
final bool isAsync = func.isAsynchronous;
@@ -382,8 +380,9 @@ $resultAt != null
382
380
'$leftHandSide = ($argsArray [$count ] as $genericArgType ?)${castCall .isEmpty ? '' : '?$castCall ' };' );
383
381
}
384
382
if (! arg.type.isNullable) {
383
+ indent.writeln ('assert($argName != null,' );
385
384
indent.writeln (
386
- "assert($ argName != null, 'Argument for $channelName was null, expected non-null $argType .');" );
385
+ " 'Argument for $channelName was null, expected non-null $argType .');" );
387
386
}
388
387
});
389
388
final Iterable <String > argNames =
@@ -446,12 +445,12 @@ $resultAt != null
446
445
codecName = _getCodecName (api);
447
446
_writeCodec (indent, codecName, api, root);
448
447
}
449
- indent.addln ( '' );
448
+ indent.newln ( );
450
449
bool first = true ;
451
450
addDocumentationComments (
452
451
indent, api.documentationComments, _docCommentSpec);
453
452
indent.write ('class ${api .name } ' );
454
- indent.scoped ('{' , '}' , () {
453
+ indent.addScoped ('{' , '}' , () {
455
454
indent.format ('''
456
455
/// Constructor for [${api .name }]. The [binaryMessenger] named argument is
457
456
/// available for dependency injection. If it is left null, the default
@@ -463,10 +462,10 @@ final BinaryMessenger? _binaryMessenger;
463
462
464
463
indent
465
464
.writeln ('static const MessageCodec<Object?> codec = $codecName ();' );
466
- indent.addln ( '' );
465
+ indent.newln ( );
467
466
for (final Method func in api.methods) {
468
467
if (! first) {
469
- indent.writeln ( '' );
468
+ indent.newln ( );
470
469
} else {
471
470
first = false ;
472
471
}
@@ -494,7 +493,7 @@ final BinaryMessenger? _binaryMessenger;
494
493
indent.write (
495
494
'Future<${_addGenericTypesNullable (func .returnType )}> ${func .name }($argSignature ) async ' ,
496
495
);
497
- indent.scoped ('{' , '}' , () {
496
+ indent.addScoped ('{' , '}' , () {
498
497
final String channelName = makeChannelName (api, func);
499
498
indent.writeln (
500
499
'final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(' );
@@ -585,7 +584,6 @@ if (replyList == null) {
585
584
dartHostTestHandler: api.dartHostTestHandler,
586
585
documentationComments: api.documentationComments,
587
586
);
588
- indent.writeln ('' );
589
587
writeFlutterApi (
590
588
generatorOptions,
591
589
root,
@@ -621,7 +619,7 @@ if (replyList == null) {
621
619
"import 'package:flutter/foundation.dart' show ReadBuffer, WriteBuffer;" );
622
620
indent.writeln ("import 'package:flutter/services.dart';" );
623
621
indent.writeln ("import 'package:flutter_test/flutter_test.dart';" );
624
- indent.writeln ( '' );
622
+ indent.newln ( );
625
623
}
626
624
}
627
625
@@ -642,33 +640,34 @@ String _getCodecName(Api api) => '_${api.name}Codec';
642
640
void _writeCodec (Indent indent, String codecName, Api api, Root root) {
643
641
assert (getCodecClasses (api, root).isNotEmpty);
644
642
final Iterable <EnumeratedClass > codecClasses = getCodecClasses (api, root);
643
+ indent.newln ();
645
644
indent.write ('class $codecName extends $_standardMessageCodec ' );
646
- indent.scoped (' {' , '}' , () {
645
+ indent.addScoped (' {' , '}' , () {
647
646
indent.writeln ('const $codecName ();' );
648
647
indent.writeln ('@override' );
649
648
indent.write ('void writeValue(WriteBuffer buffer, Object? value) ' );
650
- indent.scoped ('{' , '}' , () {
649
+ indent.addScoped ('{' , '}' , () {
651
650
enumerate (codecClasses, (int index, final EnumeratedClass customClass) {
652
651
final String ifValue = 'if (value is ${customClass .name }) ' ;
653
652
if (index == 0 ) {
654
653
indent.write ('' );
655
654
}
656
655
indent.add (ifValue);
657
- indent.scoped ('{' , '} else ' , () {
656
+ indent.addScoped ('{' , '} else ' , () {
658
657
indent.writeln ('buffer.putUint8(${customClass .enumeration });' );
659
658
indent.writeln ('writeValue(buffer, value.encode());' );
660
659
}, addTrailingNewline: false );
661
660
});
662
- indent.scoped ('{' , '}' , () {
661
+ indent.addScoped ('{' , '}' , () {
663
662
indent.writeln ('super.writeValue(buffer, value);' );
664
663
});
665
664
});
666
- indent.writeln ( '' );
665
+ indent.newln ( );
667
666
indent.writeln ('@override' );
668
667
indent.write ('Object? readValueOfType(int type, ReadBuffer buffer) ' );
669
- indent.scoped ('{' , '}' , () {
668
+ indent.addScoped ('{' , '}' , () {
670
669
indent.write ('switch (type) ' );
671
- indent.scoped ('{' , '}' , () {
670
+ indent.addScoped ('{' , '}' , () {
672
671
for (final EnumeratedClass customClass in codecClasses) {
673
672
indent.write ('case ${customClass .enumeration }: ' );
674
673
indent.writeScoped ('' , '' , () {
@@ -677,7 +676,7 @@ void _writeCodec(Indent indent, String codecName, Api api, Root root) {
677
676
});
678
677
}
679
678
indent.writeln ('default:' );
680
- indent.scoped ( '' , '' , () {
679
+ indent.nest ( 1 , () {
681
680
indent.writeln ('return super.readValueOfType(type, buffer);' );
682
681
});
683
682
});
0 commit comments