Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 转换LiveData时的类型干扰问题<;资源<;用户>&燃气轮机;到LiveData<;用户>;_Android_Kotlin_Type Inference_Android Databinding_Android Livedata - Fatal编程技术网

Android 转换LiveData时的类型干扰问题<;资源<;用户>&燃气轮机;到LiveData<;用户>;

Android 转换LiveData时的类型干扰问题<;资源<;用户>&燃气轮机;到LiveData<;用户>;,android,kotlin,type-inference,android-databinding,android-livedata,Android,Kotlin,Type Inference,Android Databinding,Android Livedata,我正在尝试将数据绑定与数据绑定结合起来。要做到这一点,我想我必须添加一个从LiveData>到LiveData的额外转换: val userResourceLiveData:LiveData=Transformations.switchMap(_login){login-> if(login==null){ AbsentLiveData.create() } 否则{ repository.loadUser(登录) } } val userLiveData:LiveData=Transformat

我正在尝试将数据绑定与数据绑定结合起来。要做到这一点,我想我必须添加一个从LiveData>到LiveData的额外转换:

val userResourceLiveData:LiveData=Transformations.switchMap(_login){login->
if(login==null){
AbsentLiveData.create()
}
否则{
repository.loadUser(登录)
}
}
val userLiveData:LiveData=Transformations.switchMap(userResourceLiveData){userResource->
if(userResource==null){//if上的错误1
AbsentLiveData.create()//关于“create()”的错误2
}
否则{
MutableLiveData(userResource.data)
}
}
但是,会显示两个错误:

1) 控制流表达式的类型推断失败,请显式指定其类型

2) 类型推断失败:信息不足,无法推断fun create()中的参数T:LiveData

如果我将代码更改为:

if (userResource == null) {
   AbsentLiveData.create<User>()
}
if(userResource==null){
AbsentLiveData.create()
}
然后
switchMap
开始抱怨:

类型推断失败:无法推断

1) 为什么不一样?我根本不希望需要类型定义,因为
的映射以同样的方式工作正常

2) 如何解决这些错误

3) 这种应用数据绑定的解决方案可能是错误的方法吗

这对我来说很有用:

        if (userResource == null) {
            AbsentLiveData.create<User>()
        }
        else {
            MutableLiveData(userResource.data!!)
        }
if(userResource==null){
AbsentLiveData.create()
}
否则{
MutableLiveData(userResource.data!!)
}

userResource.data
是类型
User?
我想说的是因为
userResource.data
的类型是
User?
,它返回
LiveData
,因此它可能与方法签名不匹配。您是否尝试过类似于
MutableLiveData(userResource.data!!)
?您是对的,它是
用户?
,但是,我在这一行中没有看到任何错误。将其更改为
MutableLiveData(userResource.data!!)
不会改变任何事情。只有我在评论中指出的地方有错误。如果您需要更多信息,可以使用我的回购。如果我省略了完整的if-else语句,那么我确实需要将其定义为
MutableLiveData(userResource.data!!)
。但是,应用程序立即在该行崩溃,因为
userResource.data
==null!我自己刚刚找到了一个不同的解决方案:
if(userResource?.data==null){AbsentLiveData.create()}否则{MutableLiveData(userResource.data)}
我认为它比强制
更安全。我注意到了这一点,因为与
userResourceLiveData
映射相比,
if
语句是唯一的区别。还是。由于您的评论,我找到了解决方案,因此我接受您的回答;-)尽管如此,我认为这个错误还是有误导性的(在我将
添加到
create()
之后,至少是后者)!
        if (userResource == null) {
            AbsentLiveData.create<User>()
        }
        else {
            MutableLiveData(userResource.data!!)
        }