{"id":639,"date":"2025-08-17T01:48:32","date_gmt":"2025-08-17T01:48:32","guid":{"rendered":"https:\/\/boochlin.com\/?p=639"},"modified":"2025-11-12T12:18:11","modified_gmt":"2025-11-12T12:18:11","slug":"kotlin-init-%e5%89%8d%e5%88%9d%e5%a7%8b%e5%8c%96%e5%b1%ac%e6%80%a7%e6%9c%83%e7%99%bc%e7%94%9f%e5%95%a5%e4%ba%8b","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=639","title":{"rendered":"kotlin init \u524d\u521d\u59cb\u5316\u5c6c\u6027\u6703\u767c\u751f\u5565\u4e8b"},"content":{"rendered":"<p><strong>Kotlin \u7684 class \u5167\u90e8\uff0c\u5728\u521d\u59cb\u5316\u968e\u6bb5\uff0c\u662f\u7121\u60c5\u5730\u7531\u4e0a\u5230\u4e0b\u57f7\u884c\u7684<\/strong>\u3002<\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-code\"><code>\/\/ \u4e0b\u9762\u9019\u6a23\u5beb\u6703\u767c\u751f\u5565\u4e8b\uff0c\u4e0d\u6703\u767c\u751f\u5565\u4e8b\uff0c\u56e0\u70ba\u7de8\u8b6f\u4e0d\u904e\uff0c\u6240\u4ee5\u4e0d\u80fd\u8dd1\nclass ItemEditViewModel(...) : ViewModel() {\n\u00a0 \u00a0 init {\n\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 itemUiState = itemsRepository.getItemStream(itemId)\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 .filterNotNull()\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 .first()\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 .toItemUiState(true)\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\n\u00a0 \u00a0 \u00a0 \u00a0 \/\/ ... use itemId ...\n\u00a0 \u00a0 }\n\/\/ <strong>\u274c \u932f\u8aa4\u793a\u7bc4<\/strong>. \u5c6c\u6027\u5f8c\u521d\u59cb\u5316\n\u00a0 \u00a0 private val itemId: Int = checkNotNull(savedStateHandle&#91;ItemEditDestination.itemIdArg])\n}\n<\/code><\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h3 class=\"wp-block-heading\">\u90a3\u6211\u52a0\u4e0a\u4e00\u500b \u975e\u540c\u6b65\u8655\u7406\uff0c\u5982\u4e0b\uff0c\u9019\u6a23\u6703\u767c\u751f\u5565\u4e8b<\/h3>\n<pre class=\"wp-block-code\"><code>init {\n\u00a0 \u00a0 \u00a0 \u00a0 viewModelScope.launch {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 itemUiState = itemsRepository.getItemStream(itemId)\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 .filterNotNull()\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 .first()\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 .toItemUiState(true)\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 }\nprivate val itemId: Int = checkNotNull(savedStateHandle&#91;ItemEditDestination.itemIdArg])\n\n\u53ef\u4ee5\u7de8\u8b6f\u904e\uff0c\u4f46\u662f\u6703\u6709\u554f\u984c\ngetItemStream \u6703\u6c38\u9060\u662f\u53ea\u8b80\u53d6\u5230\uff10\uff0e\u4e3b\u8981\u662f\u56e0\u70ba Int \u7684\u9810\u8a2d\u503c\u5c31\u662f0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin \u7684 class \u5167\u90e8\uff0c\u5728\u521d\u59cb\u5316\u968e\u6bb5\uff0c\u662f\u7121\u60c5\u5730\u7531\u4e0a\u5230\u4e0b\u57f7\u884c\u7684\u3002 Kotlin \/\/ \u4e0b\u9762\u9019\u6a23\u5beb\u6703\u767c\u751f\u5565\u4e8b\uff0c\u4e0d\u6703\u767c\u751f\u5565\u4e8b\uff0c\u56e0\u70ba\u7de8\u8b6f\u4e0d\u904e\uff0c\u6240\u4ee5\u4e0d\u80fd\u8dd1 class ItemEditViewMod&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/639"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=639"}],"version-history":[{"count":9,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/639\/revisions"}],"predecessor-version":[{"id":759,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/639\/revisions\/759"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}