admin管理员组文章数量:1635678
近来学习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()}")
本文标签: 报错navigationcomposedestinationNavGraph
版权声明:本文标题:Compose 报错 navigation destination is not a direct child of this NavGraph 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729214180a1190224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论