简体   繁体   English

如何预览具有非空类型参数的可组合项?

[英]How to preview a Composable with non-null type parameters?

@Composable
fun VerifyScreen(
    email: String,
    navigator: DestinationsNavigator
) {
// .. 

}


@Composable
@Preview
fun VerifyScreenPreview() {
    VerifyScreen("you@re.awesome", null)
}

Null can not be a value of a non-null type DestinationsNavigator Null 不能是非空类型 DestinationsNavigator 的值

Of course you can change the type by adding a question mark, but what if that is not an option?当然,您可以通过添加问号来更改类型,但如果这不是一个选项怎么办?

navigator: DestinationsNavigator?

You can use EmptyDestinationsNavigator provided by the library您可以使用库提供的EmptyDestinationsNavigator

@Composable
@Preview
fun VerifyScreenPreview() {
    VerifyScreen("you@re.awesome", EmptyDestinationsNavigator)
}

It's discouraged to pass NavController or anything like that to your Composables, see the decumentation .不鼓励将NavController或类似的东西传递给您的可组合项,请参阅文档 If you follow the recommendation, you will simply pass empty lambda (or more of them) from your Preview如果您遵循建议,您只需从Preview中传递空的 lambda(或更多)

Pass lambdas that should be triggered by the composable to navigate, rather than the NavController itself.传递应由可组合项触发以进行导航的 lambda,而不是 NavController 本身。

The problem with that is when you need a lot of lambdas in your Composable, it doesn't look very nice.这样做的问题是,当您的 Composable 中需要大量 lambda 时,它看起来不太好。 One option then is not to preview such a big Composable as a whole but rather preview some smaller pieces of the ui.一种选择是不预览如此大的 Composable 作为一个整体,而是预览 ui 的一些较小部分。 The other option would be to create some interface for the DestinationsNavigator that you can mock from your previews.另一种选择是为您可以从预览中模拟的DestinationsNavigator创建一些界面。

You can use This pattern您可以使用此模式

@Composable
fun VerifyScreen(
    email: String,
    navigator: DestinationsNavigator
) {
    VerifyScreenContent(
        email = email,
        navigator = {
            //...
        },
        navigator2 = {
            //...
        },
        navigator3 = {
            //...
        },
    )
}

@Composable
fun VerifyScreenContent(
    email: String,
    navigator: () -> Unit,
    navigator2: () -> Unit,
    navigator3: () -> Unit,
) {
// .. 

}


@Composable
@Preview
fun VerifyScreenContentPreview() {
    VerifyScreen("you@re.awesome", {}, {}, {})
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决“NullPointerException:null 不能转换为非空类型 android.graphics.Bitmap”? - How can I solve "NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap"? 如何让 .getCurrentUser() 返回非空? - How to make .getCurrentUser() return non-null? kotlin.TypeCastException:null 不能转换为非 null 类型 - kotlin.TypeCastException: null cannot be cast to non-null type Android Room将Null返回为Non-Null类型 - Android Room returns Null as Non-Null type Retrofit - 删除为 null 但响应主体类型声明为非空 - Retrofit - delete was null but response body type was declared as non-null PlaceAutocompleteFragment-无法将null强制转换为非null类型(Kotlin) - PlaceAutocompleteFragment - null cannot be cast to non-null type (Kotlin) 房间迁移 - 如何向现有表添加新的非空和字符串类型列 - Room Migration- How to add a new NON-NULL and String type column to existing table 预览中不支持具有非默认参数的可组合函数,除非它们使用 @PreviewParameter 注释 - Composable functions with non-default parameters are not supported in Preview unless they are annotated with @PreviewParameter 非空对象类型上的 Retrofit-Kotlin NullPointerException - Retrofit-Kotlin NullPointerException on non-null object type 警告:“onCreateView”总是返回非空类型 - Warning: 'onCreateView' always returns non-null type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM