本文介绍了LiveData的ViewModel SavedStateHandler.get无法根据文档设置默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在引用
String defaultValue = ...; // nullable
LiveData<String> liveData;
if (defaultValue != null) {
liveData = savedStateHandle.get(KEY, defaultValue);
} else {
liveData = savedStateHandle.get(KEY);
}
但是,我注意到在尝试编译代码时,下面的语句是
However, I notice that when tried to compile the code, the statement below is not compilable.
savedStateHandle.get(KEY, defaultValue);
错误指出
get(String) in SavedStateHandle cannot be applied to (String, java.lang.String).
我跟踪代码,看起来像 savedStatehandle
没有使用默认值的 get
。我错过了什么吗?
I trace into the code, and seems like savedStatehandle
doesn't have a get
that takes in a default value. Did I miss anything?
推荐答案
显然Google文档有错字。
Apparently the Google Document has typo. It supposed to be
String defaultValue = ...; // nullable
LiveData<String> liveData;
if (defaultValue != null) {
liveData = savedStateHandle.getLiveData(KEY, defaultValue);
} else {
liveData = savedStateHandle.getLiveData(KEY);
}
即 getLiveData
而不只是 get
。
这篇关于LiveData的ViewModel SavedStateHandler.get无法根据文档设置默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!