Home > front end >  Compose: Is there an EmptyView similar to Swiftui?
Compose: Is there an EmptyView similar to Swiftui?

Time:01-19

On iOS there is EmptyView here https://developer.apple.com/documentation/swiftui/emptyview. But I don't know how to implement it on Compose. If I have it, for some code is much easier for me. For example,

myList.map { item ->
   if item is XItem -> EmptyView()
   ....
}

Don't tell me I need not it, I just know how to implement it. Thanks.

CodePudding user response:

Compose is built much different than SwiftUI.

In SwiftUI you need to use EmptyView in two cases:

  1. When you have a genetic parameter and it should be empty in some cases - e.g. you need to define some default type in case when the parameter is not specified.
  2. When the context requires you to return some view.

On the other side, Compose doesn't have such problems in the first place, that's why no such view exists.

In cases when SwiftUI will give you an error around an empty @ViewBuilder block, Compose will be totally fine.

In your example you can use Unit:

myList.map { item ->
   if item is XItem -> Unit
   ....
}

Or just empty braces:

myList.map { item ->
   if item is XItem -> { }
   ....
}

If you'll find a case when you really need some empty view, you can use Box(Modifier) - it'll be an empty view with zero size.

CodePudding user response:

I think you can use a Spacer component to display an empty space.

Spacer accepts Modifier object as a parameter, you can then use this modifier to set Spacer’s width or height or both.

For instance, you can draw a Spacer in your code but this needs to be done in a composable context or inside another composable.

  @Composable
    fun MyComposable(){
    
    myList.map { item ->
       if item is XItem -> Spacer(modifier = Modifier.size(100.dp, 100.dp))
       ....
    }
}
  •  Tags:  
  • Related