Home > Software design >  Kotlin / value passing to List<>()
Kotlin / value passing to List<>()

Time:10-08

I have a question in List<Contact>() I'm asked to pass init and size. I'm not sure if it's obligated to pass it as in my following tutorial ArrayList<String>() was empty, maybe it's because I was using List<>? Also, it doesn't recognize lowercase() and add() is it also related to List<>?

Code Snippet

val contacts = remember { DataProvider.contactList }
var filteredContacts: List<Contact>
val textState = remember { mutableStateOf(TextFieldValue("")) }
LazyColumn(
    ...
) {
    val searchText = textVal.value.text
    filteredContacts = if (searchText.isEmpty()){
        contacts
    }
    else{
        val resultList = List<Contact>()
        for (contact in contacts) {
            if (contact.lowercase(Locale.getDefault()).contains(searchText.lowercase(Locale.getDefault()))) {
                resultList.add(contact)
            }
        }
        resultList
    }

CodePudding user response:

In kotlin, List has no add method. For that you would need to have a MutableList. Regarding lowercase method, this is available for Strings. You are trying to apply that to a Contact object, which I guess has no lowercase method.

  • Related