https://kotlinlang.org logo
#compose
Title
# compose
c

camkadev

04/24/2020, 3:15 PM
var authState by state { AuthState() }
complaint about
Functions which invoke @Composable functions must be marked with the @Composable annotation
what should i do?
z

Zach Klippenstein (he/him) [MOD]

04/24/2020, 3:47 PM
Can you post a larger code snippet for context?
c

codeslubber

04/24/2020, 4:12 PM
Sounds like maybe they added an error message that was not there before. Is AuthState composable?
c

camkadev

04/24/2020, 4:21 PM
AuthState is my model class
Copy code
@Model
class AuthState(
    var login: String = "",
    var password: String = "",
    var token: String = ""
)
c

codeslubber

04/24/2020, 4:21 PM
I thought state and model were either ors?
z

Zach Klippenstein (he/him) [MOD]

04/24/2020, 4:24 PM
You usually use
remember
with
@Model
classes because you want to keep the same instance of the model (
state
is for when you want to change the reference itself), but it should still compile. Can you post the code around
var authState by state { AuthState() }
?
l

Leland Richardson [G]

04/24/2020, 4:56 PM
if AuthState is a model you very likely do not want state there. That said, I am fairly sure that this is unrelated to the error that you’re getting?
are you trying to use
state
outside of a composable function?
c

camkadev

04/24/2020, 6:39 PM
@Zach Klippenstein (he/him) [MOD] it’s just a var’s placed outside of any class @Leland Richardson [G] i use authState inside Composable methods
z

Zach Klippenstein (he/him) [MOD]

04/24/2020, 6:40 PM
Ah, if you’re trying to call
state
for a top-level var, that’s the problem.
state
is a composable function, it can only be called from inside another composable function, just like the error says.
Copy code
// Not allowed
var foo by state { "foo" }

@Composable fun Bar() {
  // Allowed
  var bar by state { "bar" }
}
c

camkadev

04/24/2020, 6:41 PM
i migrated from dev05 (to dev09) where that was works
z

Zach Klippenstein (he/him) [MOD]

04/24/2020, 6:42 PM
Huh, I don’t even know what
state
at a top level would me or how that would work. Curious what Leland has to say 😅
c

camkadev

04/24/2020, 6:43 PM
i replaced
by state
with
= mutableStateOf(...)
and looks like that works, but any way found compilation error https://issuetracker.google.com/issues/154867792
z

Zach Klippenstein (he/him) [MOD]

04/24/2020, 6:54 PM
mutableStateOf
isn’t a composable function so it should be callable from anywhere.
The difference is in who “owns” the
State
instance – if you call
mutableStateOf
from the top level, it’s a global variable and owned by the process, and will never be GC’d. If you use
state
(or
remember { mutableStateOf() }
which is how
state
works under the hood), the instance is owned by the composable at that point in the composition tree, and will be GC’d if that composable ever leaves the composition.
c

camkadev

04/24/2020, 7:06 PM
ok thanks for explanation
l

Leland Richardson [G]

04/24/2020, 7:22 PM
yep.
state
is just shorthand for
remember { mutableStateOf(block()) }
👍🏻 1
2 Views