Flutter json_serializable詳細(xì)使用(小白教程)

重點:后面就用了json_serializable這個解析工具,可能看起來有些麻煩,但是也有情況會出現(xiàn)類型轉(zhuǎn)換的問題,這時候你就要認(rèn)真去對比一下服務(wù)器數(shù)據(jù)跟解析出來的數(shù)據(jù)類型了。

(1)關(guān)于JsonBeanFactory

JsonBeanFactory的可以查看這篇文章:FlutterJsonBeanFactory插件json使用

插件就是上面的這個截圖

(2)json_serializable的使用

(1)yaml配置文件關(guān)聯(lián)

? json_serializable: ^3.0.0

? build_runner: ^1.6.1


可以在https://pub.dev/packages/這個庫進行搜索,使用最新的版本。

(2)打開網(wǎng)站,建議收藏:https://caijinglong.github.io/json2dart/index.html


上面圖說了步驟:這里再說一下,選擇v1.x.x,然后命名一個類,在工程中創(chuàng)建一個dart文件(如圈出來的3),然后把你的json放到第四個位置里,得到的右邊的便是你所要的bean對象了。

(3)把bean對象張貼進去,這時候會報錯,此時你不需要進行操作,只需要進行下面的第四步操作。

(4)Terminal運行:flutter packages pub run build_runner build,自動生成g.dart

在工程的Terminal下運行代碼: 這是一個實時監(jiān)控bean中類型變化的。主要依賴庫build_runner

flutter packages pub run build_runner build

輸出內(nèi)容

D:\AS\Flutter>flutter packages pub run build_runner watch

[INFO] Generating build script...

[INFO] Generating build script completed, took 274ms

[INFO] Setting up file watchers...

[INFO] Setting up file watchers completed, took 8ms

[INFO] Waiting for all file watchers to be ready...

[INFO] Waiting for all file watchers to be ready completed, took 329ms

[INFO] Initializing inputs

[INFO] Reading cached asset graph...

[INFO] Reading cached asset graph completed, took 91ms

[INFO] Checking for updates since last build...

[INFO] Checking for updates since last build completed, took 2.1s

[INFO] Running build...

[INFO] Running build completed, took 21ms

[INFO] Caching finalized dependency graph...

[INFO] Caching finalized dependency graph completed, took 47ms

[INFO] Succeeded after 73ms with 0 outputs (0 actions)

如圖:


(5) 下面就看見我們生成的bean的對象,以及g.dart文件


看下g.dart文件


以上就是個人小白親自操作教程,暫且就這么多。

————————————————

版權(quán)聲明:本文為CSDN博主「xudailong_blog」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/xudailong_blog/article/details/95168949

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容