简体   繁体   English

我得到“类型不匹配。 必需:State<string?> 使用 Jetpack 组合 viewModel.observeAsState() 时发现:字符串</string?>

[英]I get “Type mismatch. Required: State<String?> Found: String” when Using viewModel.observeAsState() with Jetpack compose

I am creating an app that takes users input in a textfield and instead of using:我正在创建一个应用程序,它需要用户在文本字段中输入,而不是使用:

var textState = remember { mutableStateOf("") }

I opted for using viewModels like我选择使用 viewModels 之类的

var title : State<String?> = addTaskViewModel.title.observeAsState()

TextField(
modifier = Modifier.fillMaxWidth(),
value = title.value!!,
onValueChange = { title = it },
shape = textFieldShape,
colors = textFieldColors,
label = {Text("Add a title",)} )

I get an error我收到一个错误

Type mismatch.类型不匹配。 Required: State<String?> Found: String必需:State<String?> 找到:String

when setting onValueChange = { title = it } on the TextField widget.在 TextField 小部件上设置onValueChange = { title = it }时。

How can I use viewModel.observeAsState() with a TextField?如何将viewModel.observeAsState()与 TextField 一起使用?

The best way is to create inside the ViewModel class a function to change this value.最好的方法是在ViewModel class 内部创建一个 function 来更改此值。

Something like:就像是:

class TaskViewModel : ViewModel() {

    //Just an example
    private var _title = MutableLiveData("")
    var title: LiveData<String> = _title

    fun onTitleChange(newName: String) {
        _title.value = newName
    }
}

and then:接着:

val title : State<String> = taskViewModel.title.observeAsState("")
TextField(
        modifier = Modifier.fillMaxWidth(),
        value = title.value,
        onValueChange = { taskViewModel.onTitleChange(it) },
        label = {Text("Add a title")} )

or:或者:

  val title: String by taskViewModel.title.observeAsState("")
  TextField(
        modifier = Modifier.fillMaxWidth(),
        value = title,
        onValueChange = { taskViewModel.onTitleChange(it) },
        label = {Text("Add a title")} )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型不匹配。 必需:字符串:找到:Int 错误 - Type mismatch. Required: String! Found: Int error 类型不匹配。 必填地图 <String, Any> 找到地图 <String, Any?> - Type mismatch. Required Map<String, Any> Found Map<String, Any?> 类型不匹配。 必需:片段,找到:PlaceAutocompleteFragment - Type mismatch. Required: Fragment, Found: PlaceAutocompleteFragment 类型不匹配。 要求:观察员<in Int!>成立:? - Type mismatch. Required: Observer<in Int!> Found:? 类型不匹配。 必需:找不到:回调&lt;*&gt; - Type mismatch. Required: Nothing Found: Callback<*> 类型不匹配。 必需:找到的上下文:在片段中 - Type mismatch. Required: Context Found: In Fragment 类型不匹配。 必需:ContentResolver! 找到:整数 - Type mismatch. Required: ContentResolver! Found: Int 类型不匹配。 必需:数组 <Uri> ! 找到:数组 <Uri?> - Type mismatch. Required: Array<Uri>! Found: Array<Uri?> 类型不匹配。 必需: suspend () → Response&lt;*&gt; 找到:Response<Void> - Type mismatch. Required: suspend () → Response<*> Found: Response<Void> 类型不匹配。 必需:结果<newsresponse> : 发现: 结果<response<newsresponse> &gt;? </response<newsresponse></newsresponse> - Type mismatch. Required: Result<NewsResponse>! Found: Result<Response<NewsResponse>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM