1

I noticed today that my preview doesn't work if I have a flow as a parameter to a Composable i.e. the below will always render as emptyMap()

@Composable
fun BakesList(
    bakes: Flow<Map<Bake, List<Ingredient>>>,
) {
    val bakesMap by bakes.collectAsState(initial = emptyMap())

    LazyColumn {
      //display columns
      //...
    }
}

If I change my Composable parameter to a Map<Bake, List<Ingredient>> then everything renders correctly. Is there a simple fix for this other than redesigning all my composables?

David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39
  • 1
    By default preview only renders the initial recomposition, and on the initial recomposition `collectAsState` always have the `initial` value. Check out [this answer](https://stackoverflow.com/a/67864552/3585796) for how you can enable interactive preview. – Phil Dukhov Apr 08 '22 at 07:34
  • Thanks I forgot about the interactive preview, it works there, but is still not smooth. do you know if there is a flag that I could use to swap out the initial value? i.e. `initialValue = if (preview) a else b` – David Aleksanyan Apr 08 '22 at 08:10
  • 2
    Yes, [this answer](https://stackoverflow.com/a/70163730/3585796) – Phil Dukhov Apr 08 '22 at 08:20

0 Answers0