如何在数据类中传递可组合内容参数

发布于 2025-01-09 09:58:16 字数 185 浏览 1 评论 0原文

我需要在数据类中传递一个撰写内容参数。例如,按钮在添加到此内容中时可以呈现。

data class ContentData {
  val content: @Composable ()-> Unit
}

这是有效的,但是当我获得应用程序背景时,我遇到了可分割的异常。如何解决这个问题。

I need to pass a compose content parameter in data class. For example a button can render when added into this content.

data class ContentData {
  val content: @Composable ()-> Unit
}

This is working but when I get the app background I am getting parcelable exception. How to solve this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2025-01-16 09:58:16

我认为一种可能的解释是,如果您尝试通过 Intent 将此类对象作为额外对象在活动之间传递,则会发生与可分割错误相关的情况。考虑不要使用 Composable 作为对象中的参数。相反,尝试使用包含参数的模型来表示可组合项的参数。

// your compose function
@Composable
fun Item(content: String = "Default", padding: Dp){
    // ...
}

// Ui Model which contains your data (instead of have a weird composable reference) as a parcelable. 
data class ContentData(
    val content: String = "Default",
    val paddingRaw: Int = 0
) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString().orEmpty(),
        parcel.readInt()
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(content)
        parcel.writeInt(paddingRaw)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ContentData> {
        override fun createFromParcel(parcel: Parcel): ContentData {
            return ContentData(parcel)
        }

        override fun newArray(size: Int): Array<ContentData?> {
            return arrayOfNulls(size)
        }
    }
}

// Example if you need the model between activities through the intent as an extra.
val data = ContentData("your content", 11)
val intent = Intent().apply {
    putExtra("keyContentData", data)
}

//The way of get and use your model.
val contentData = intent.extras?.get("keyContentData") as ContentData

@Composable
fun ParentComponent(){
    // ...

        Item(
            contentData?.content.orEmpty(),
            contentData?.paddingRaw?.dp ?: 0.dp
        )
    // ...
}

One possible explanation I think that will occur related with a parcelable error, happens if you try to pass such object between activities as extras through Intent. Consider not use Composable as parameters in objects. Instead, try to represent the parameters of your Composable with a model which contains the parameters.

// your compose function
@Composable
fun Item(content: String = "Default", padding: Dp){
    // ...
}

// Ui Model which contains your data (instead of have a weird composable reference) as a parcelable. 
data class ContentData(
    val content: String = "Default",
    val paddingRaw: Int = 0
) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString().orEmpty(),
        parcel.readInt()
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(content)
        parcel.writeInt(paddingRaw)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ContentData> {
        override fun createFromParcel(parcel: Parcel): ContentData {
            return ContentData(parcel)
        }

        override fun newArray(size: Int): Array<ContentData?> {
            return arrayOfNulls(size)
        }
    }
}

// Example if you need the model between activities through the intent as an extra.
val data = ContentData("your content", 11)
val intent = Intent().apply {
    putExtra("keyContentData", data)
}

//The way of get and use your model.
val contentData = intent.extras?.get("keyContentData") as ContentData

@Composable
fun ParentComponent(){
    // ...

        Item(
            contentData?.content.orEmpty(),
            contentData?.paddingRaw?.dp ?: 0.dp
        )
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文