admin管理员组

文章数量:1635683

近来学习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