Compose 报错 navigation destination is not a direct child of this NavGraph

近来学习Compose时在使用Navigation参数传递的时候出现了一个报错,花了一番功夫。
首先因为Navigation参数传递本身就是以URL的形式进行传递的所以我们在一个URL里面套一个URL必定会出现解析 / 错误

Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/article_page/https://juejin.cn/post/7096376474831650823 } 
cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x177a1068) route=main_page}

发现其中系统的

android-app://androidx.navigation

与我们的文章URL重叠所以导致参数解析异常无法定位到此URL结局办法也很简单使用

URLEncoder.encode(this, StandardCharsets.UTF_8.toString())

将其加密以下就会将/即可替换为% 为了方便使用定义以下工具类

import java.net.URLDecoder
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

/**
 * When you want to pass the URL in the URL,
 * please pay attention to the encoding format,
 * otherwise it will cause / be parsed by Navigation as the system directed URL.
 * Please use encode to encode the URL to avoid this problem
 * create by zyique chou 05/11/2022
 */

fun String.encode() = URLEncoder.encode(this, StandardCharsets.UTF_8.toString()) ?: ""

fun String.decode() = URLDecoder.decode(this, StandardCharsets.UTF_8.toString()) ?: ""

在需要的地方进行调用即可

navController.navigate(ApplicationPages.ARTICLE_PAGE.router + "/${article.link.encode()}")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值