@@ -32,6 +32,7 @@ class KotlinOptions {
32
32
this .package,
33
33
this .copyrightHeader,
34
34
this .errorClassName,
35
+ this .includeErrorClass = true ,
35
36
});
36
37
37
38
/// The package where the generated class will live.
@@ -43,13 +44,20 @@ class KotlinOptions {
43
44
/// The name of the error class used for passing custom error parameters.
44
45
final String ? errorClassName;
45
46
47
+ /// Whether to include the error class in generation.
48
+ ///
49
+ /// This should only ever be set to false if you have another generated
50
+ /// Kotlin file in the same directory.
51
+ final bool includeErrorClass;
52
+
46
53
/// Creates a [KotlinOptions] from a Map representation where:
47
54
/// `x = KotlinOptions.fromMap(x.toMap())` .
48
55
static KotlinOptions fromMap (Map <String , Object > map) {
49
56
return KotlinOptions (
50
57
package: map['package' ] as String ? ,
51
58
copyrightHeader: map['copyrightHeader' ] as Iterable <String >? ,
52
59
errorClassName: map['errorClassName' ] as String ? ,
60
+ includeErrorClass: map['includeErrorClass' ] as bool ? ?? true ,
53
61
);
54
62
}
55
63
@@ -60,6 +68,7 @@ class KotlinOptions {
60
68
if (package != null ) 'package' : package! ,
61
69
if (copyrightHeader != null ) 'copyrightHeader' : copyrightHeader! ,
62
70
if (errorClassName != null ) 'errorClassName' : errorClassName! ,
71
+ 'includeErrorClass' : includeErrorClass,
63
72
};
64
73
return result;
65
74
}
@@ -298,7 +307,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
298
307
addDocumentationComments (
299
308
indent, field.documentationComments, _docCommentSpec);
300
309
indent.write (
301
- 'val ${field .name }: ${_nullsafeKotlinTypeForDartType (field .type )}' );
310
+ 'val ${field .name }: ${_nullSafeKotlinTypeForDartType (field .type )}' );
302
311
final String defaultNil = field.type.isNullable ? ' = null' : '' ;
303
312
indent.add (defaultNil);
304
313
}
@@ -361,16 +370,15 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
361
370
});
362
371
});
363
372
364
- final String errorClassName = _getErrorClassName (generatorOptions);
365
373
for (final Method method in api.methods) {
366
374
_writeFlutterMethod (
367
375
indent,
376
+ generatorOptions: generatorOptions,
368
377
name: method.name,
369
378
parameters: method.parameters,
370
379
returnType: method.returnType,
371
380
channelName: makeChannelName (api, method, dartPackageName),
372
381
documentationComments: method.documentationComments,
373
- errorClassName: errorClassName,
374
382
dartPackageName: dartPackageName,
375
383
);
376
384
}
@@ -588,7 +596,9 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
588
596
if (hasFlutterApi) {
589
597
_writeCreateConnectionError (generatorOptions, indent);
590
598
}
591
- _writeErrorClass (generatorOptions, indent);
599
+ if (generatorOptions.includeErrorClass) {
600
+ _writeErrorClass (generatorOptions, indent);
601
+ }
592
602
}
593
603
594
604
static void _writeMethodDeclaration (
@@ -606,7 +616,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
606
616
final List <String > argSignature = < String > [];
607
617
if (parameters.isNotEmpty) {
608
618
final Iterable <String > argTypes = parameters
609
- .map ((NamedType e) => _nullsafeKotlinTypeForDartType (e.type));
619
+ .map ((NamedType e) => _nullSafeKotlinTypeForDartType (e.type));
610
620
final Iterable <String > argNames = indexMap (parameters, getArgumentName);
611
621
argSignature.addAll (
612
622
map2 (
@@ -620,7 +630,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
620
630
}
621
631
622
632
final String returnTypeString =
623
- returnType.isVoid ? '' : _nullsafeKotlinTypeForDartType (returnType);
633
+ returnType.isVoid ? '' : _nullSafeKotlinTypeForDartType (returnType);
624
634
625
635
final String resultType = returnType.isVoid ? 'Unit' : returnTypeString;
626
636
addDocumentationComments (indent, documentationComments, _docCommentSpec);
@@ -700,7 +710,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
700
710
indent.write ('$call ' );
701
711
final String resultType = returnType.isVoid
702
712
? 'Unit'
703
- : _nullsafeKotlinTypeForDartType (returnType);
713
+ : _nullSafeKotlinTypeForDartType (returnType);
704
714
indent.addScoped ('{ result: Result<$resultType > ->' , '}' , () {
705
715
indent.writeln ('val error = result.exceptionOrNull()' );
706
716
indent.writeScoped ('if (error != null) {' , '}' , () {
@@ -751,11 +761,11 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
751
761
752
762
void _writeFlutterMethod (
753
763
Indent indent, {
764
+ required KotlinOptions generatorOptions,
754
765
required String name,
755
766
required List <Parameter > parameters,
756
767
required TypeDeclaration returnType,
757
768
required String channelName,
758
- required String errorClassName,
759
769
required String dartPackageName,
760
770
List <String > documentationComments = const < String > [],
761
771
int ? minApiRequirement,
@@ -778,6 +788,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
778
788
getArgumentName: _getSafeArgumentName,
779
789
);
780
790
791
+ final String errorClassName = _getErrorClassName (generatorOptions);
781
792
indent.addScoped ('{' , '}' , () {
782
793
onWriteBody (
783
794
indent,
@@ -910,9 +921,9 @@ String _kotlinTypeForBuiltinGenericDartType(TypeDeclaration type) {
910
921
} else {
911
922
switch (type.baseName) {
912
923
case 'List' :
913
- return 'List<${_nullsafeKotlinTypeForDartType (type .typeArguments .first )}>' ;
924
+ return 'List<${_nullSafeKotlinTypeForDartType (type .typeArguments .first )}>' ;
914
925
case 'Map' :
915
- return 'Map<${_nullsafeKotlinTypeForDartType (type .typeArguments .first )}, ${_nullsafeKotlinTypeForDartType (type .typeArguments .last )}>' ;
926
+ return 'Map<${_nullSafeKotlinTypeForDartType (type .typeArguments .first )}, ${_nullSafeKotlinTypeForDartType (type .typeArguments .last )}>' ;
916
927
default :
917
928
return '${type .baseName }<${_flattenTypeArguments (type .typeArguments )}>' ;
918
929
}
@@ -946,7 +957,7 @@ String _kotlinTypeForDartType(TypeDeclaration type) {
946
957
return _kotlinTypeForBuiltinDartType (type) ?? type.baseName;
947
958
}
948
959
949
- String _nullsafeKotlinTypeForDartType (TypeDeclaration type) {
960
+ String _nullSafeKotlinTypeForDartType (TypeDeclaration type) {
950
961
final String nullSafe = type.isNullable ? '?' : '' ;
951
962
return '${_kotlinTypeForDartType (type )}$nullSafe ' ;
952
963
}
@@ -969,7 +980,7 @@ String _cast(Indent indent, String variable, {required TypeDeclaration type}) {
969
980
}
970
981
return '${type .baseName }.ofRaw($variable as Int)!!' ;
971
982
}
972
- return '$variable as ${_nullsafeKotlinTypeForDartType (type )}' ;
983
+ return '$variable as ${_nullSafeKotlinTypeForDartType (type )}' ;
973
984
}
974
985
975
986
String _castInt (bool isNullable) {
0 commit comments