Skip to content

Commit 1d925d7

Browse files
committed
Fix conflicts with same method names across different modules on iOS
This is done by prefixig method names with the module name
1 parent 1558ddd commit 1d925d7

File tree

9 files changed

+251
-282
lines changed

9 files changed

+251
-282
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## unreleased
44

5+
- Fix conflicts with same method names across different modules on iOS by prefixing method names
6+
57
## v0.1.0-rc.2
68

79
- Fix errors with boolean types in method return and flow types on iOS

example/flutter/example/lib/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ class _MyAppState extends State<MyApp> {
192192

193193
final mySecondTestModule = MySecondTestModule();
194194

195+
print(
196+
"myTestModule.methodWithSameNameAsInOtherModule: ${await myTestModule.methodWithSameNameAsInOtherModule("abc")}");
197+
print(
198+
"mySecondTestModule.methodWithSameNameAsInOtherModule: ${await mySecondTestModule.methodWithSameNameAsInOtherModule(123)}");
199+
195200
print(
196201
"mySecondTestModule.testMethod: ${await mySecondTestModule.testMethod()}");
197202

example/flutter/lib/MySecondTestModule.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final Stream<MyDataClass> dataClassEvents = const EventChannel('MySecondTestModu
2020
Future<String> testMethod() async {
2121

2222
final invokeResult = await methodChannelToNative.invokeMethod<String>(
23-
'testMethod',
23+
'MySecondTestModule_testMethod',
2424
[],
2525
);
2626

@@ -32,4 +32,19 @@ final Stream<MyDataClass> dataClassEvents = const EventChannel('MySecondTestModu
3232

3333
return result;
3434
}
35+
Future<int> methodWithSameNameAsInOtherModule(int value) async {
36+
37+
final invokeResult = await methodChannelToNative.invokeMethod<int>(
38+
'MySecondTestModule_methodWithSameNameAsInOtherModule',
39+
[value],
40+
);
41+
42+
if (invokeResult == null) {
43+
throw PlatformException(code: '1', message: 'Method methodWithSameNameAsInOtherModule failed');
44+
}
45+
46+
final result = invokeResult;
47+
48+
return result;
49+
}
3550
}

example/flutter/lib/MyTestModule.dart

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final Stream<MyDataClass> dataClassEvents = const EventChannel('MyTestModule_dat
2525

2626
Future<int?> next(int? previous) async {
2727
return await methodChannelToNative.invokeMethod<int>(
28-
'intState',
28+
'MyTestModule_intState',
2929
[previous]
3030
);
3131
}
@@ -60,7 +60,7 @@ final Stream<MyDataClass> dataClassEvents = const EventChannel('MyTestModule_dat
6060

6161
Future<String?> next(String? previous) async {
6262
return await methodChannelToNative.invokeMethod<String>(
63-
'dataClassState',
63+
'MyTestModule_dataClassState',
6464
[previous]
6565
);
6666
}
@@ -95,7 +95,7 @@ final Stream<MyDataClass> dataClassEvents = const EventChannel('MyTestModule_dat
9595

9696
Future<String?> next(String? previous) async {
9797
return await methodChannelToNative.invokeMethod<String>(
98-
'parameterizedDataClassState',
98+
'MyTestModule_parameterizedDataClassState',
9999
[previous, dataSerialized]
100100
);
101101
}
@@ -130,7 +130,7 @@ final Stream<MyDataClass> dataClassEvents = const EventChannel('MyTestModule_dat
130130

131131
Future<String?> next(String? previous) async {
132132
return await methodChannelToNative.invokeMethod<String>(
133-
'boolState',
133+
'MyTestModule_boolState',
134134
[previous]
135135
);
136136
}
@@ -165,7 +165,7 @@ final Stream<MyDataClass> dataClassEvents = const EventChannel('MyTestModule_dat
165165

166166
Future<int?> next(int? previous) async {
167167
return await methodChannelToNative.invokeMethod<int>(
168-
'intStateAdd',
168+
'MyTestModule_intStateAdd',
169169
[previous, num]
170170
);
171171
}
@@ -196,12 +196,12 @@ final Stream<MyDataClass> dataClassEvents = const EventChannel('MyTestModule_dat
196196
}
197197
Future<void> unitMethod() async {
198198

199-
await methodChannelToNative.invokeMethod<void>('unitMethod', []);
199+
await methodChannelToNative.invokeMethod<void>('MyTestModule_unitMethod', []);
200200
}
201201
Future<String> simpleMethod() async {
202202

203203
final invokeResult = await methodChannelToNative.invokeMethod<String>(
204-
'simpleMethod',
204+
'MyTestModule_simpleMethod',
205205
[],
206206
);
207207

@@ -216,7 +216,7 @@ Future<String> simpleMethod() async {
216216
Future<String> stringMethod(String value) async {
217217

218218
final invokeResult = await methodChannelToNative.invokeMethod<String>(
219-
'stringMethod',
219+
'MyTestModule_stringMethod',
220220
[value],
221221
);
222222

@@ -231,7 +231,7 @@ Future<String> stringMethod(String value) async {
231231
Future<String?> nullableStringMethod(String? value) async {
232232

233233
final invokeResult = await methodChannelToNative.invokeMethod<String?>(
234-
'nullableStringMethod',
234+
'MyTestModule_nullableStringMethod',
235235
[value],
236236
);
237237
final result = invokeResult;
@@ -240,7 +240,7 @@ Future<String?> nullableStringMethod(String? value) async {
240240
Future<int> intMethod(int value) async {
241241

242242
final invokeResult = await methodChannelToNative.invokeMethod<int>(
243-
'intMethod',
243+
'MyTestModule_intMethod',
244244
[value],
245245
);
246246

@@ -255,7 +255,7 @@ Future<int> intMethod(int value) async {
255255
Future<double> doubleMethod(double value) async {
256256

257257
final invokeResult = await methodChannelToNative.invokeMethod<double>(
258-
'doubleMethod',
258+
'MyTestModule_doubleMethod',
259259
[value],
260260
);
261261

@@ -270,7 +270,7 @@ Future<double> doubleMethod(double value) async {
270270
Future<bool> boolMethod(bool value) async {
271271
final valueSerialized = value.toString();
272272
final invokeResult = await methodChannelToNative.invokeMethod<String>(
273-
'boolMethod',
273+
'MyTestModule_boolMethod',
274274
[valueSerialized],
275275
);
276276

@@ -282,15 +282,30 @@ Future<bool> boolMethod(bool value) async {
282282

283283
return result;
284284
}
285+
Future<String> methodWithSameNameAsInOtherModule(String value) async {
286+
287+
final invokeResult = await methodChannelToNative.invokeMethod<String>(
288+
'MyTestModule_methodWithSameNameAsInOtherModule',
289+
[value],
290+
);
291+
292+
if (invokeResult == null) {
293+
throw PlatformException(code: '1', message: 'Method methodWithSameNameAsInOtherModule failed');
294+
}
295+
296+
final result = invokeResult;
297+
298+
return result;
299+
}
285300
Future<void> parameterizedMethod(String a, int b, bool c, double d) async {
286301
final cSerialized = c.toString();
287-
await methodChannelToNative.invokeMethod<void>('parameterizedMethod', [a, b, cSerialized, d]);
302+
await methodChannelToNative.invokeMethod<void>('MyTestModule_parameterizedMethod', [a, b, cSerialized, d]);
288303
}
289304
Future<DateTime> localDateTimeMethod(DateTime localDateTime) async {
290305
if (localDateTime.isUtc) throw ArgumentError('localDateTime must not be in UTC');
291306
final localDateTimeSerialized = localDateTime.toIso8601String();
292307
final invokeResult = await methodChannelToNative.invokeMethod<String>(
293-
'localDateTimeMethod',
308+
'MyTestModule_localDateTimeMethod',
294309
[localDateTimeSerialized],
295310
);
296311

@@ -305,7 +320,7 @@ final localDateTimeSerialized = localDateTime.toIso8601String();
305320
Future<TimeOfDay> localTimeMethod(TimeOfDay localTime) async {
306321
final localTimeSerialized = "${localTime.hour.toString().padLeft(2, '0')}:${localTime.minute.toString().padLeft(2, '0')}";
307322
final invokeResult = await methodChannelToNative.invokeMethod<String>(
308-
'localTimeMethod',
323+
'MyTestModule_localTimeMethod',
309324
[localTimeSerialized],
310325
);
311326

@@ -321,7 +336,7 @@ Future<DateTime> localDateMethod(DateTime localDate) async {
321336
if (localDate.isUtc) throw ArgumentError('localDate must not be in UTC');
322337
final localDateSerialized = localDate.toIso8601String().split('T').first;
323338
final invokeResult = await methodChannelToNative.invokeMethod<String>(
324-
'localDateMethod',
339+
'MyTestModule_localDateMethod',
325340
[localDateSerialized],
326341
);
327342

@@ -336,7 +351,7 @@ final localDateSerialized = localDate.toIso8601String().split('T').first;
336351
Future<Duration> durationMethod(Duration duration) async {
337352
final durationSerialized = duration.toIso8601String();
338353
final invokeResult = await methodChannelToNative.invokeMethod<String>(
339-
'durationMethod',
354+
'MyTestModule_durationMethod',
340355
[durationSerialized],
341356
);
342357

@@ -352,7 +367,7 @@ Future<DateTime> instantMethod(DateTime instant) async {
352367
if (!instant.isUtc) throw ArgumentError('instant must be in UTC');
353368
final instantSerialized = instant.toIso8601String();
354369
final invokeResult = await methodChannelToNative.invokeMethod<String>(
355-
'instantMethod',
370+
'MyTestModule_instantMethod',
356371
[instantSerialized],
357372
);
358373

@@ -367,7 +382,7 @@ final instantSerialized = instant.toIso8601String();
367382
Future<List<String>> stringListMethod(List<String> list) async {
368383
final listSerialized = jsonEncode(list.map((e) => e).toList());
369384
final invokeResult = await methodChannelToNative.invokeMethod<String>(
370-
'stringListMethod',
385+
'MyTestModule_stringListMethod',
371386
[listSerialized],
372387
);
373388

@@ -384,7 +399,7 @@ return element as String;
384399
Future<List<List<String>>> nestedListMethod(List<List<String>> list) async {
385400
final listSerialized = jsonEncode(list.map((e) => e.map((e) => e).toList()).toList());
386401
final invokeResult = await methodChannelToNative.invokeMethod<String>(
387-
'nestedListMethod',
402+
'MyTestModule_nestedListMethod',
388403
[listSerialized],
389404
);
390405

@@ -403,7 +418,7 @@ return element as String;
403418
Future<List<MyDataClass>> dataClassListMethod(List<MyDataClass> list) async {
404419
final listSerialized = jsonEncode(list.map((e) => e).toList());
405420
final invokeResult = await methodChannelToNative.invokeMethod<String>(
406-
'dataClassListMethod',
421+
'MyTestModule_dataClassListMethod',
407422
[listSerialized],
408423
);
409424

@@ -420,7 +435,7 @@ return MyDataClass.fromJson(element);
420435
Future<List<List<MyDataClass>>> nestedDataClassListMethod(List<List<MyDataClass>> list) async {
421436
final listSerialized = jsonEncode(list.map((e) => e.map((e) => e).toList()).toList());
422437
final invokeResult = await methodChannelToNative.invokeMethod<String>(
423-
'nestedDataClassListMethod',
438+
'MyTestModule_nestedDataClassListMethod',
424439
[listSerialized],
425440
);
426441

@@ -439,7 +454,7 @@ return MyDataClass.fromJson(element);
439454
Future<Map<String, int>> mapMethod(Map<String, int> map) async {
440455
final mapSerialized = jsonEncode(map.map((k, v) => MapEntry(k, v)));
441456
final invokeResult = await methodChannelToNative.invokeMethod<String>(
442-
'mapMethod',
457+
'MyTestModule_mapMethod',
443458
[mapSerialized],
444459
);
445460

@@ -456,7 +471,7 @@ return MapEntry(key, value as int);
456471
Future<MyDataObject> objectMethod(MyDataObject obj) async {
457472
final objSerialized = jsonEncode(obj.toJson());
458473
final invokeResult = await methodChannelToNative.invokeMethod<String>(
459-
'objectMethod',
474+
'MyTestModule_objectMethod',
460475
[objSerialized],
461476
);
462477

@@ -471,7 +486,7 @@ Future<MyDataObject> objectMethod(MyDataObject obj) async {
471486
Future<MySealedData> sealedMethod(MySealedData obj) async {
472487
final objSerialized = jsonEncode(MySealedData.toJson(obj));
473488
final invokeResult = await methodChannelToNative.invokeMethod<String>(
474-
'sealedMethod',
489+
'MyTestModule_sealedMethod',
475490
[objSerialized],
476491
);
477492

@@ -486,7 +501,7 @@ Future<MySealedData> sealedMethod(MySealedData obj) async {
486501
Future<MyDateClass> dateClassMethod(MyDateClass obj) async {
487502
final objSerialized = jsonEncode(obj.toJson());
488503
final invokeResult = await methodChannelToNative.invokeMethod<String>(
489-
'dateClassMethod',
504+
'MyTestModule_dateClassMethod',
490505
[objSerialized],
491506
);
492507

@@ -501,7 +516,7 @@ Future<MyDateClass> dateClassMethod(MyDateClass obj) async {
501516
Future<MySealedDataWithProps> sealedWithPropsMethod(MySealedDataWithProps obj) async {
502517
final objSerialized = jsonEncode(MySealedDataWithProps.toJson(obj));
503518
final invokeResult = await methodChannelToNative.invokeMethod<String>(
504-
'sealedWithPropsMethod',
519+
'MyTestModule_sealedWithPropsMethod',
505520
[objSerialized],
506521
);
507522

@@ -516,7 +531,7 @@ Future<MySealedDataWithProps> sealedWithPropsMethod(MySealedDataWithProps obj) a
516531
Future<MyDataClassWithSealed> classWithSealedPropMethod(MyDataClassWithSealed obj) async {
517532
final objSerialized = jsonEncode(obj.toJson());
518533
final invokeResult = await methodChannelToNative.invokeMethod<String>(
519-
'classWithSealedPropMethod',
534+
'MyTestModule_classWithSealedPropMethod',
520535
[objSerialized],
521536
);
522537

@@ -531,7 +546,7 @@ Future<MyDataClassWithSealed> classWithSealedPropMethod(MyDataClassWithSealed ob
531546
Future<MyEnum> enumMethod(MyEnum entry) async {
532547
final entrySerialized = jsonEncode(entry.name);;
533548
final invokeResult = await methodChannelToNative.invokeMethod<String>(
534-
'enumMethod',
549+
'MyTestModule_enumMethod',
535550
[entrySerialized],
536551
);
537552

@@ -546,7 +561,7 @@ Future<MyEnum> enumMethod(MyEnum entry) async {
546561
Future<List<MyEnum>> enumListMethod(List<MyEnum> entries) async {
547562
final entriesSerialized = jsonEncode(entries.map((e) => e.name).toList());
548563
final invokeResult = await methodChannelToNative.invokeMethod<String>(
549-
'enumListMethod',
564+
'MyTestModule_enumListMethod',
550565
[entriesSerialized],
551566
);
552567

@@ -563,7 +578,7 @@ return MyEnum.values.byName(element);
563578
Future<Map<String, MyEnum>> enumMapMethod(Map<String, MyEnum> entries) async {
564579
final entriesSerialized = jsonEncode(entries.map((k, v) => MapEntry(k, v.name)));
565580
final invokeResult = await methodChannelToNative.invokeMethod<String>(
566-
'enumMapMethod',
581+
'MyTestModule_enumMapMethod',
567582
[entriesSerialized],
568583
);
569584

@@ -580,7 +595,7 @@ return MapEntry(key, MyEnum.values.byName(value));
580595
Future<Map<String, List<Map<String, MyDataClass>>>> mixedMethod(Map<String, List<Map<String, MyDataClass>>> map) async {
581596
final mapSerialized = jsonEncode(map.map((k, v) => MapEntry(k, v.map((e) => e.map((k, v) => MapEntry(k, v))).toList())));
582597
final invokeResult = await methodChannelToNative.invokeMethod<String>(
583-
'mixedMethod',
598+
'MyTestModule_mixedMethod',
584599
[mapSerialized],
585600
);
586601

@@ -601,7 +616,7 @@ return MapEntry(key, MyDataClass.fromJson(value));
601616
Future<MyDataClass> dataClassMethod(MyDataClass data) async {
602617
final dataSerialized = jsonEncode(data.toJson());
603618
final invokeResult = await methodChannelToNative.invokeMethod<String>(
604-
'dataClassMethod',
619+
'MyTestModule_dataClassMethod',
605620
[dataSerialized],
606621
);
607622

@@ -615,12 +630,12 @@ Future<MyDataClass> dataClassMethod(MyDataClass data) async {
615630
}
616631
Future<void> suspendUnitMethod() async {
617632

618-
await methodChannelToNative.invokeMethod<void>('suspendUnitMethod', []);
633+
await methodChannelToNative.invokeMethod<void>('MyTestModule_suspendUnitMethod', []);
619634
}
620635
Future<String> suspendStringMethod() async {
621636

622637
final invokeResult = await methodChannelToNative.invokeMethod<String>(
623-
'suspendStringMethod',
638+
'MyTestModule_suspendStringMethod',
624639
[],
625640
);
626641

example/src/commonMain/kotlin/com/example/flutterkmpexample/MyTestModule.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class MyTestModule(coroutineScope: CoroutineScope) {
6464
@FlutterMethod
6565
fun boolMethod(value: Boolean): Boolean = value
6666

67+
@FlutterMethod
68+
fun methodWithSameNameAsInOtherModule(value: String): String = value
69+
6770
@FlutterMethod
6871
fun parameterizedMethod(
6972
a: String,
@@ -241,6 +244,9 @@ class MySecondTestModule(coroutineScope: CoroutineScope) {
241244
@FlutterMethod
242245
fun testMethod(): String = "Hello from Kotlin!!!"
243246

247+
@FlutterMethod
248+
fun methodWithSameNameAsInOtherModule(value: Int): Int = value
249+
244250
@FlutterFlow
245251
val intEvents: Flow<Int> = _intState
246252

0 commit comments

Comments
 (0)