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()}")
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)