camkadev
04/24/2020, 3:15 PMvar authState by state { AuthState() }
complaint about Functions which invoke @Composable functions must be marked with the @Composable annotation
what should i do?Zach Klippenstein (he/him) [MOD]
04/24/2020, 3:47 PMcodeslubber
04/24/2020, 4:12 PMcamkadev
04/24/2020, 4:21 PM@Model
class AuthState(
var login: String = "",
var password: String = "",
var token: String = ""
)
codeslubber
04/24/2020, 4:21 PMZach Klippenstein (he/him) [MOD]
04/24/2020, 4:24 PMremember
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() }
?Leland Richardson [G]
04/24/2020, 4:56 PMstate
outside of a composable function?camkadev
04/24/2020, 6:39 PMZach Klippenstein (he/him) [MOD]
04/24/2020, 6:40 PMstate
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.// Not allowed
var foo by state { "foo" }
@Composable fun Bar() {
// Allowed
var bar by state { "bar" }
}
camkadev
04/24/2020, 6:41 PMZach Klippenstein (he/him) [MOD]
04/24/2020, 6:42 PMstate
at a top level would me or how that would work. Curious what Leland has to say 😅camkadev
04/24/2020, 6:43 PMby state
with = mutableStateOf(...)
and looks like that works, but any way found compilation error
https://issuetracker.google.com/issues/154867792Zach Klippenstein (he/him) [MOD]
04/24/2020, 6:54 PMmutableStateOf
isn’t a composable function so it should be callable from anywhere.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.camkadev
04/24/2020, 7:06 PMLeland Richardson [G]
04/24/2020, 7:22 PMstate
is just shorthand for remember { mutableStateOf(block()) }