【问题标题】:Returning a list as a result to method channel is throwing java.lang.illegalArgumentException?将列表作为结果返回到方法通道会引发 java.lang.illegalArgumentException?
【发布时间】:2020-12-30 08:26:49
【问题描述】:
 override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler{ call, result ->
            if (call.method == "getBatteryLevel") {
               val batteryLevel = getBatteryLevel()
                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null)
                }
            } else if(call.method == "getWifiConnectionList"){
                val ans : List<ScanResult> = getWifiScanResults(context)
              result.success(ans)
            } else {
                result.notImplemented()
            }
        }
    }

我正在尝试使用方法通道发送可用的 wifi 网络。我是 Kotlin 新手,请帮帮我

   private fun getWifiScanResults(context: Context): List<ScanResult> {
        val mWifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
        Toast.makeText(context, mWifiManager.scanResults[0].SSID, Toast.LENGTH_LONG).show()
        return mWifiManager.scanResults
    }

我试图从列表中取出一个项目并将其发送到颤振它可以工作,但是当我发送可用 wifi 网络列表时它会引发错误。

E/MethodChannel#com.codever: Failed to handle method call
    java.lang.IllegalArgumentException: Unsupported value: SSID: Rahul Dubey , BSSID: 34:e8:94:28:57:3a, capabilities: [WPA2-PSK-CCMP][RSN-PSK-CCMP][ESS], level: -59, frequency: 2462, timestamp: 51104155586, distance: ?(cm), distanceSd: ?(cm), passpoint: no, ChannelBandwidth: 1, centerFreq0: 2452, centerFreq1: 0, 80211mcResponder: is not supported, Carrier AP: no, Carrier AP EAP Type: -1, Carrier name: null, Radio Chain Infos: null, wifiMode: 4, semVendorSpecificInfo: null, semBssLoadElement: 040000127A
        at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:278)
        at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:267)
        at io.flutter.plugin.common.StandardMethodCodec.encodeSuccessEnvelope(StandardMethodCodec.java:59)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:235)
        at com.example.rahul_app.MainActivity$configureFlutterEngine$1.onMethodCall(MainActivity.kt:40)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
        at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
        at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:336)
        at android.os.Looper.loop(Looper.java:197)
        at android.app.ActivityThread.main(ActivityThread.java:8125)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

【问题讨论】:

    标签: java android kotlin flutter-platform-channel flutter-method-channel


    【解决方案1】:

    我们无法返回类类型,而是将其转换为字符串并使用 json.decode() 方法在颤振中对其进行解码。

    【讨论】:

      猜你喜欢
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 2022-01-04
      • 1970-01-01
      相关资源
      最近更新 更多