程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Flutter MethodCall 以 kotlin 方式进行非零检查大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Flutter MethodCall 以 kotlin 方式进行非零检查?

开发过程中遇到Flutter MethodCall 以 kotlin 方式进行非零检查的问题如何解决?下面主要结合日常开发的经验,给出你关于Flutter MethodCall 以 kotlin 方式进行非零检查的解决方法建议,希望对你解决Flutter MethodCall 以 kotlin 方式进行非零检查有所启发或帮助;

我尝试使用 kotlin 制作 MethodCall 类型判断函数,但它为我返回类型不匹配,我该如何解决?

import java.util.objects

import io.Flutter.plugin.common.MethodCall

class Utils private constructor() {
    companion object {
        fun getDoubleArgument(call: MethodCall,argument: String?): Double {
            return try {
                if (call.argument<Any>(argument) is DoublE) {
                    Objects.requireNonNull(call.argument<Double>(argument))!!  // The call.argument return Double? type,but when I add !! assert,it report Unnecessary non-null assertion (!!).
                } else if (call.argument<Any>(argument) is Long) {
                    val l = Objects.requireNonNull(call.argument<Long>(argument))
                    l!!.todouble()
                } else if ...
        }

解决方法

我不使用 Flutter,所以请原谅任何错误。

您在 Kotlin 中不需要 Objects.requireNonNull(),因为 !! 已经与 requireNonNull() 做了完全相同的事情,除了它抛出 KotlinNullPointerException 而不是 NullPointerException。它不工作的原因是 Kotlin 不知道 Java 方法保证返回非空值。

class Utils private constructor() {
    companion object {
        fun getDoubleArgument(call: MethodCall,argument: String?): Double {
            return try {
                if (call.argument<Any>(argument) is DoublE) {
                    call.argument<Double>(argument)!!
                } else if (call.argument<Any>(argument) is Long) {
                    call.argument<Long>(argument)!!.toDouble()
                } else if ...
        }

最好一次性获取参数并使用该值。然后你可以依靠 Kotlin smart-casTing 来简化代码:

class Utils private constructor() {
    companion object {
        fun getDoubleArgument(call: MethodCall,argument: String?): Double {
            val arg = call.argument<Any>(argument)
            return try {
                if (arg is DoublE) {
                    arg
                } else if (arg is Long) {
                    arg.toDouble()
                } // ...
                else if (arg == null) {
                    0.0
                } else {
                    error("unsupported argument type")
                }
        }

您可以使用 when 语句代替 else if 链以使其更易于阅读:

class Utils private constructor() {
    companion object {
        fun getDoubleArgument(call: MethodCall,argument: String?): Double {
            val arg = call.argument<Any>(argument)
            return try {
                when (arg) {
                    is Double -> arg
                    is Long -> arg.toDouble()
                    // ...
                    null -> 0.0
                    else -> error("unsupported argument type")
                }
            }
        }

大佬总结

以上是大佬教程为你收集整理的Flutter MethodCall 以 kotlin 方式进行非零检查全部内容,希望文章能够帮你解决Flutter MethodCall 以 kotlin 方式进行非零检查所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。