跳到主要內容

發表文章

目前顯示的是 七月, 2021的文章

Flutter: JSON Parser example

Flutter的Json處理真是有夠難... 看了老半天還是看不太懂, 挫折感重重. 他有兩套方法一個是dart內建的 dart:convert ,另一個是套件,其號稱較容易處理複雜的JSON結構. === 一個外國網友說:  I have to admit, I was missing the gson world of Android after working with JSON in Flutter/Dart. When I started working with APIs in Flutter, JSON parsing really had me struggle a lot. And I’m certain, it confuses a lot of you beginners. by Pooja Bhaumik 大家可以去讀這篇json parse in flutter文章 ps:額外閱讀: 30天Flutter手滑系列 - JSON與序列化(JSON and serialization)  --- Is there a GSON/ Jackson/ Moshi equivalent in Flutter? The simple answer is no.  似乎是因為 程式庫 反射(run-time reflection)的關係,使得Flutter 無法對其做檔案最小化.  ---- 在android我們用gson太久,太方便,所以被寵壞了. 一換到flutter後,沒有gson,我們就不會用了. 寫程式這種技能,就跟啃饅頭一樣,很簡單,但你還得一口一口慢慢啃完. 對,沒有自己寫出一段json parser範例code,你就是算不會他. 一定要自己寫出來,結果有跑出來才算,然後在熟習其用法.等到真實專案遇到了就不會害怕. --- Flutter有內建 d art:convert library可用來parse JSON. "為較小的項目使用手動序列化數據",但這是一個很基本的library. Basic JSON serialization in Flutter is very simple. Flutter has a built-in dart:convert library that includes

Flutter Web renderer: doesn't show Image on the chrome browser but works on android.

Flutter可以跑在chrome瀏覽器上,但一執行卻出現無法顯示圖片的錯誤訊息. 這到底是怎麼了? 太奇怪了吧,花了半天檢查程式碼是否有寫錯. Image . network ( "https://flutter.cn/assets/flutter-lockup-1caf6476beed76adec3c477586da54de6b552b2f42108ec5bc68dc63bae2df75.png" ), 語法是對的,但跑在瀏覽器上就是會出現大叉叉. When the exception was thrown, this was the stack: Image provider: CachedNetworkImageProvider("https://via.placeholder.com/150/a46a91", scale: 1) Image key: CachedNetworkImageProvider("https://via.placeholder.com/150/a46a91", scale: 1) ===== Exception caught by image resource service == The following ImageCodecException was thrown resolving an image codec: Failed to load network image. Image URL: https://via.placeholder.com/150/323599 Trying to load an image from another domain? ---- 然後我才發現,同樣的flutter code,但在android 平台上可以跑(右圖). 但放在電腦chrome 卻不能正常執行,顯示的圖片上出現驚嘆號(左圖). 原來不是程式寫錯. 原因似乎在這: flutter web has upgraded default rendered(HTML) -> CanvasKit and it has better performance. https://flutter.dev/docs/development/platform-integra