How Do I Get My Project To Recognize Remember and MutableStateOf? - android

I am trying to make use of the following code:
var a by remember {
mutableStateOf("")
}
When I put that code in my project. Both remember and mutableStateOf is red. My project doesn't recognize what they are. I think I have to import something or add a dependency, but I am not sure what or how to do this.

I think you forgot imports.
by it is kotlin operator. You have to import it.
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue

Related

Type 'State<List<User>?>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate

I'm trying to get a value from LiveData with observeAsState in jetpack compose, but I get a weird error
Type 'State<List?>' has no method 'getValue(Nothing?,
KProperty<*>)' and thus it cannot serve as a delegate
Code
#Composable
fun UserScreen(userViewModel:UserViewModel){
val items: List<User> by userViewModel.fetchUserList.observeAsState()
UserList(userList = items)
}
ViewModel
class UserViewModel: ViewModel() {
private val dataSource = UserDataSource()
val fetchUserList = liveData {
emit(dataSource.dummyUserList)
}
}
If you get a compiler error that observeAsState or getValue are not defined make sure you have the following imports:
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
This information is from Step #4 in the "Using State in Jetpack Compose" codelab.
To fix the error add the following imports:
// for a 'val' variable
import androidx.compose.runtime.getValue
// for a `var` variable also add
import androidx.compose.runtime.setValue
// or just
import androidx.compose.runtime.*
To use a variable as a property delegate you should provide getValue operator function for read-only val variables and getValue and setValue functions for var variables.
To read more about how property delegates and state are combined in jetpack compose see Use remember to create internal state in a composable documentation section. There's also an explanation in Thinking in Compose video.
You could use: import androidx.compose.runtime.*
Necessary imports are:
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
var value by remember { mutableStateOf("") }
For me manually/explicitly importing both the below apis worked to resolve this compilation issue,
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
Here is the reference,
https://developer.android.com/jetpack/compose/state#state-in-composables
I think type of items must be nullable since you observing LiveData:
val items: List<User>? by userViewModel.fetchUserList.observeAsState()
Adding a dependency fixed the problem for me:
implementation "androidx.compose.runtime:runtime:$compose_version"
Thanks to Tayaab Mizhar
You need to import
import androidx.compose.runtime.getValue
which will import this function which is basically by
inline operator fun <T> State<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value
Just add this line manually below all the import libraries.
import androidx.compose.runtime.*
In my case, in a compose application it was this missing import that provoc the error
import androidx.compose.getValue
I had this problem despite imports! Took a bit but then I realised where my problem was the variable for whatever you're observing needs to be a val not a var:
In my case it was
var background: Int by currentBackgroundColor.observeAsState(0)
Should have been:
val background: Int by currentBackgroundColor.observeAsState(0)

ArgumentMatchers.any must not be null

I am trying to test ViewModel to make sure livedata gets updated correctly. However when using ArgumentMatchers.any() it fails with IllegalStateException saying:
ArgumentMatchers.any(mViewModel.CountSubscriber::class.java) must not
be null
#Test
fun emitValueIfCountIs7() {
doAnswer { invocation: InvocationOnMock ->
val subscriber: mViewModel.CountSubscriber = invocation.getArgument(0)
subscriber.onNext(7)
null
}.`when`(countUseCase).execute(
ArgumentMatchers.any(mViewModel.CountSubscriber::class.java),
ArgumentMatchers.any(Parameters::class.java)
)
// When
mViewModel.getCount()
// Verify
assert(mViewModel.countResponse.value != null)
}
I am using Kotlin and have the following dependencies:
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-inline:2.23.4"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"
Here are my imports:
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.nhaarman.mockitokotlin2.doAnswer
import io.reactivex.Observable
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.mockito.invocation.InvocationOnMock
Strange thing is that it used to work before, and I don't know what has happened that could affect this.
Getting matchers to work with Kotlin can be a problem.
If you have a method written in kotlin that does not take a nullable parameter, then we cannot match with it using Mockito.any().
This is because it can return void and this is not assignable to a non-nullable parameter.
If the method being matched is written in Java, then I think that it will work as all Java objects are implicitly nullable.
One possible solution would be to use a library like mockito-kotlin
But you can solve this issue easily with a few lines of code yourself.
If you need typed any(type: Class)
private fun <T> any(type: Class<T>): T = Mockito.any<T>(type)
OR
You can use this matcher instead of Matchers.any() :
object MockitoHelper {
fun <T> anyObject(): T {
Mockito.any<T>()
return uninitialized()
}
#Suppress("UNCHECKED_CAST")
fun <T> uninitialized(): T = null as T
}
and use MockitoHelper.anyObject() instead of any() in your kotlin tests.
You can find more information in this post: Using Mockito with Kotlin
There is a discussion about possible solutions in this post :
Is it possible to use Mockito in Kotlin?
The correct solution is mentioned in the comment section of the question by #Ana Koridze. Yes, if you are using Koltin + mockitoKotlin. Make sure you are using the following import:
1 - Using the Mockito-kotlin:
import org.mockito.kotlin.any from Mockito-kotlin
instead of
import org.mockito.Mockito.any
testImplementation "org.mockito.kotlin:mockito-kotlin:$mockito_kotlin_version"
2 - Or if you are using older mockito kotlin version original created by nhaarman before the intergation
import com.nhaarman.mockitokotlin2.any from nhaaram's Mockito-kotlin instead of import org.mockito.Mockito.any
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:$mockito_kotlin2_version"
BTW, if you are using Android Studio or IntelliJ IDEA. the any() from mockitokotlin library should be italic by default style/color scheme.
Notice the any() at the end of line. This is from mockitokotlin
And here is the any() from mockito
Thanks #Sattar for the recommended edit.
mockito-kotlin has added support for nullable args with anyOrNull()
`when`(myMock.doesThis(any(), anyOrNull())).thenReturn(someObject)
use Mockito-kotlin
testImplementation "org.mockito.kotlin:mockito-kotlin:$mockito_kotlin_version"
this will use any that works for kotlin as this is a wrapper lib for Mockito but for kotlin
This is what worked for me,
either replaced all generic any()s , with a specific anyTYPE(), i.e anyInt(), anyList() etc from core lib org.mockito:mockito-core and it fixes the (nullability)issue, it seems the specific definitions with type can handle null. this option does not require you to import any extra lib
or
if you really need to use the generic type any() , add this official Mckito extension lib https://github.com/mockito/mockito-kotlin and make sure you use the any() from this lib (by making sure your imports has this in it import org.mockito.kotlin.any)
I suggest to start using MockK lib https://github.com/mockk/mockk instead of Mockito as it is a Mock library for Kotlin = MockK)
however, if you feel lazy to switch right now or maybe dealing with legacy tests (as in my case :), this should fix your issue too.
For me all solutions above were not enough - in addition to that I had to mark the called method as an 'open' method.
According to this:
https://github.com/mockito/mockito-kotlin/wiki/Parameter-specified-as-non-null-is-null
The method is final and Mockito couldn't mock it so I had to add 'open'.
I wrote a simple wrapper function around Mockito's any() and got rid of the warning.
private fun <T> any() : T {
return org.mockito.ArgumentMatchers.any()
}
I am still pretty green at Kotlin though, so I am not sure whether there may be some unwanted side effects. I put this answer out there in case it helps anyone or someone gives me feedback.

Jetpack Compose: Exception while analyzing expression

I am trying to build simple app using Jetpack Compose.
I followed this documentation, downloaded repository and created my own module.
Code is pretty simple:
import android.app.Activity
import android.os.Bundle
import androidx.compose.Composable
import androidx.ui.core.Text
import androidx.ui.core.setContent
import androidx.ui.material.surface.Card
import androidx.ui.graphics.Color
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApp()
}
}
#Composable
fun MyApp() {
Card(color = Color.Cyan) {
Text("test")
}
}
}
But I noticed that some composable widgets doesnt work and I have following error:
Exception while analyzing expression at (23,9) in
/path/Projects/androidx-master-dev/frameworks/support/ui/compose/src/main/java/app/myown/MainActivity.kt
Where (23,9) references to Card widget
By the way other widgets work, for example I dont have problems with
#Composable
fun MyApp() {
Padding(10.dp) {
Text("test")
}
}
It compiles and runs perfectly.
I got following problem with:
Card
Column
Row
Center
FlexColumn
and I guess many others widgets
I ran into this problem earlier.
There is an implicit need to have import androidx.compose.composer in every Kotlin source file that has #Composable functions. I say "implicit" because Android Studio thinks that it is unnecessary and has a tendency to remove that line (e.g., you ask it to optimize imports). Some #Composable functions can survive without this import, but others cannot.
As I understand it, this is one of those things that will get better as the libraries and tooling evolve, but at the moment, just keep an eye out for that import and add it if it is missing and you are getting weirder-than-normal results.

unresloved reference to Select.from activeandroid

i have code
fun loadAllNotes() : MutableList<Note> = Select.from(Note::class.java).fetch()
but i have trouble with Select.from: Unresolved reference: from.
It's code by guide and it works in tutorial.
How can i fix it?
need to use
import com.reactiveandroid.query.Select
not
import com.activeandroid.query.Select

How to call my function directly in Chaquopy?

How to call a function I wrote using Chaquopy?
I used sample from documentation and it worked well. I could put the text on a label using Python. Then I created a button and want to call my function (for example, my_func):
from demo.chaquopy.pythonactivity import R
from java import static_proxy, Override, jvoid
from android.os import Bundle
from android.support.v7.app import AppCompatActivity
class MainActivity(static_proxy(AppCompatActivity)):
#Override(jvoid, [Bundle])
def onCreate(self, state):
AppCompatActivity.onCreate(self, state)
self.setContentView(R.layout.activity_main)
self.findViewById(R.id.label).setText("Hello From Python!")
def my_func(self):
self.findViewById(R.id.label).setText("Another text")
I tried to write 'android:onclick' as with Java functions, and it doesn't work. And I couldn't find it in samples and documentation. Please can anyone help? Thanks a lot in advance.
To declare a listener in XML, the method has to be visible from Java. You can make it visible like this:
from java import method
from android.view import View
class MainActivity(...):
#method(jvoid, [View])
def my_func(self):
...

Categories

Resources