如何在Swift原生代码中调用Flutter中的参数?

我正试图在Flutter中使用PlatformViews在我的Flutter应用中原生显示Swift代码,然而我的应用在我当前的代码中崩溃。

这是我的AppDelegate,目前我正在调用我的方法通道。

import Foundation

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, TJPlacementDelegate {
    var p = TJPlacement()
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        let channelName = "NativeView"
        let rootViewController : FlutterViewController = window?.rootViewController as! FlutterViewController
        let methodChannel = FlutterMethodChannel(name: channelName, binaryMessenger: rootViewController as! FlutterBinaryMessenger)
        methodChannel.setMethodCallHandler {(call: FlutterMethodCall, result: FlutterResult) -> Void in
            if (call.method == "setDebugEnabled") {
                let isDebug = call.arguments as! Bool
                Tapjoy.setDebugEnabled(isDebug)
            } 
        }
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

这是我的Dart实现的原生代码。

 import 'package:flutter/material.dart';
 import 'tapjoy.dart';

    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }

    class _HomeState extends State<Home> {
      @override
      void initState() {
        callTapjoy();
        super.initState();
      }

      Widget build(context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('Test'),
            ),
            body: UiKitView(viewType: 'NativeView'),
          ),
        );
      }

      void callTapjoy() {
        Tapjoy.setDebugEnabled(true);
      }
    }

//My code in tapjoy.dart
    class Tapjoy {
      static const MethodChannel _channel = const MethodChannel('NativeView');
          static void setDebugEnabled(bool isDebug) {
        _channel.invokeMethod('setDebugEnabled', {"isDebug": isDebug});
      } 
    } 

我的应用程序崩溃了,并在调试控制台显示错误。

Could not cast value of type '__NSDictionaryM' (0x7fff87a61d78) to 'NSNumber' (0x7fff87b1eb08).
2020-04-29 16:56:42.985269+0530 Runner[18484:224162] Could not cast value of type '__NSDictionaryM' (0x7fff87a61d78) to 'NSNumber' (0x7fff87b1eb08).

enter image description here

解决方案:

你正在传递一个 Map 从Dart到本地。{"isDebug": isDebug}所以你需要在Swift端从mapdictionary中提取参数。

  if let args = call.arguments as? Dictionary<String, Any>,
    let isDebug = args["isDebug"] as? Bool {
      // please check the "as" above  - wasn't able to test
      // handle the method

    result(nil)
  } else {
    result(FlutterError.init(code: "errorSetDebug", message: "data or format error", details: nil))
  }

或者,只需要从Dart端传递布尔值,而不需要先把它放到map中。

_channel.invokeMethod('setDebugEnabled', isDebug);

本文来自投稿,不代表实战宝典立场,如若转载,请注明出处:https://www.shizhanbaodian.com/11236.html

(0)
上一篇 2022年6月18日 上午5:30
下一篇 2022年6月18日 上午5:30

相关推荐

发表评论

登录后才能评论