@@ -949,6 +949,9 @@ if (wrapped == nil) {
949949 let identifier = self.readValue()
950950 let instance: AnyObject? = pigeonRegistrar.instanceManager.instance(
951951 forIdentifier: identifier is Int64 ? identifier as! Int64 : Int64(identifier as! Int32))
952+ if instance == nil {
953+ print("Failed to find instance with identifier: \\ (identifier!)")
954+ }
952955 return instance
953956 default:
954957 return super.readValue(ofType: type)
@@ -1502,7 +1505,7 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
15021505 }
15031506 indent.addScoped ('else {' , '}' , () {
15041507 if (returnType.isVoid) {
1505- indent.writeln ('completion(.success(Void ()))' );
1508+ indent.writeln ('completion(.success(()))' );
15061509 } else {
15071510 final String fieldType = _swiftTypeForDartType (returnType);
15081511 _writeGenericCasting (
@@ -2318,63 +2321,70 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
23182321 .failure(
23192322 ${_getErrorClassName (generatorOptions )}(
23202323 code: "ignore-calls-error",
2321- message: "Calls to Dart are being ignored.", details: "")))
2322- return''' ,
2324+ message: "Calls to Dart are being ignored.", details: "")))''' ,
23232325 );
23242326 },
2327+ addTrailingNewline: false ,
23252328 );
23262329
23272330 indent.writeScoped (
2328- 'if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {' ,
2331+ ' else if pigeonRegistrar.instanceManager.containsInstance(pigeonInstance as AnyObject) {' ,
23292332 '}' ,
23302333 () {
2331- indent.writeln ('completion(.success(Void()))' );
2332- indent.writeln ('return' );
2334+ indent.writeln ('completion(.success(()))' );
23332335 },
2336+ addTrailingNewline: false ,
23342337 );
2335- if (api.hasCallbackConstructor ()) {
2336- indent.writeln (
2337- 'let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance(pigeonInstance as AnyObject)' ,
2338- );
2339- enumerate (api.unattachedFields, (int index, ApiField field) {
2340- final String argName = _getSafeArgumentName (index, field);
2338+ indent.writeScoped (' else {' , '}' , () {
2339+ if (api.hasCallbackConstructor ()) {
23412340 indent.writeln (
2342- 'let $ argName = try! pigeonDelegate.${ field . name }(pigeonApi: self, pigeonInstance: pigeonInstance )' ,
2341+ 'let pigeonIdentifierArg = pigeonRegistrar.instanceManager.addHostCreatedInstance( pigeonInstance as AnyObject )' ,
23432342 );
2344- });
2345- indent.writeln (
2346- 'let binaryMessenger = pigeonRegistrar.binaryMessenger' ,
2347- );
2348- indent.writeln ('let codec = pigeonRegistrar.codec' );
2349- _writeFlutterMethodMessageCall (
2350- indent,
2351- generatorOptions: generatorOptions,
2352- parameters: < Parameter > [
2353- Parameter (
2354- name: 'pigeonIdentifier' ,
2355- type: const TypeDeclaration (
2356- baseName: 'int' ,
2357- isNullable: false ,
2343+ enumerate (api.unattachedFields, (int index, ApiField field) {
2344+ final String argName = _getSafeArgumentName (index, field);
2345+ indent.writeln (
2346+ 'let $argName = try! pigeonDelegate.${field .name }(pigeonApi: self, pigeonInstance: pigeonInstance)' ,
2347+ );
2348+ });
2349+ indent.writeln (
2350+ 'let binaryMessenger = pigeonRegistrar.binaryMessenger' ,
2351+ );
2352+ indent.writeln ('let codec = pigeonRegistrar.codec' );
2353+ _writeFlutterMethodMessageCall (
2354+ indent,
2355+ generatorOptions: generatorOptions,
2356+ parameters: < Parameter > [
2357+ Parameter (
2358+ name: 'pigeonIdentifier' ,
2359+ type: const TypeDeclaration (
2360+ baseName: 'int' ,
2361+ isNullable: false ,
2362+ ),
23582363 ),
2364+ ...api.unattachedFields.map (
2365+ (ApiField field) {
2366+ return Parameter (name: field.name, type: field.type);
2367+ },
2368+ ),
2369+ ],
2370+ returnType: const TypeDeclaration .voidDeclaration (),
2371+ channelName: makeChannelNameWithStrings (
2372+ apiName: api.name,
2373+ methodName: newInstanceMethodName,
2374+ dartPackageName: dartPackageName,
23592375 ),
2360- ...api.unattachedFields.map (
2361- (ApiField field) {
2362- return Parameter (name: field.name, type: field.type);
2363- },
2364- ),
2365- ],
2366- returnType: const TypeDeclaration .voidDeclaration (),
2367- channelName: makeChannelNameWithStrings (
2368- apiName: api.name,
2369- methodName: newInstanceMethodName,
2370- dartPackageName: dartPackageName,
2371- ),
2372- );
2373- } else {
2374- indent.writeln (
2375- 'print("Error: Attempting to create a new Dart instance of ${api .name }, but the class has a nonnull callback method.")' ,
2376- );
2377- }
2376+ );
2377+ } else {
2378+ indent.format (
2379+ '''
2380+ completion(
2381+ .failure(
2382+ ${_getErrorClassName (generatorOptions )}(
2383+ code: "new-instance-error",
2384+ message: "Error: Attempting to create a new Dart instance of ${api .name }, but the class has a nonnull callback method.", details: "")))''' ,
2385+ );
2386+ }
2387+ });
23782388 });
23792389
23802390 if (unsupportedPlatforms != null ) {
0 commit comments