日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Flutter】Dart 技巧 ( 独立主函数入口 | 可空类型判定 | 默认值设定 )

發(fā)布時間:2025/6/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flutter】Dart 技巧 ( 独立主函数入口 | 可空类型判定 | 默认值设定 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 一、獨立主函數(shù)入口
  • 二、可空類型判定
  • 三、默認值設定
  • 四、完整代碼示例
  • 五、 相關資源





一、獨立主函數(shù)入口



在 dart 文件中聲明 main() 函數(shù) , 即可脫離 Flutter 環(huán)境 , 獨立執(zhí)行該 main 函數(shù) ;

/// 可脫離 Flutter 環(huán)境獨立執(zhí)行的函數(shù) void main(){print("main"); }

main 函數(shù)左側有兩個三角的圖標 , 單擊該按鈕 , 即可執(zhí)行該 main 函數(shù) ;





二、可空類型判定



可空類型判定 : 不確定對象是否為空 , 通過 ?. 的方式調用 , ( 類似于 Kotlin 的可空類型調用 ) , ? 的作用是先判定是否為空 , 如果為空 , 就終止調用 , 不會報空指針異常 ;

/// 可脫離 Flutter 環(huán)境獨立執(zhí)行的函數(shù) void main(){print("main");Student student;/// 1. 安全調用 : 不確定對象是否為空 , 通過 ?. 的方式調用/// ( 類似于 Kotlin 的可空類型調用 )/// ? 的作用是先判定是否為空 , 如果為空 , 就終止調用print("打印 student 名字 : ${student?.name}");}class Student{String name;int age;Student(this.name, this.age); }

執(zhí)行結果 :

main 打印 student 名字 : null



三、默認值設定



使用 ?? 可以為某個空值設置一個默認值 , 如果某個值沒有獲取到 , 或者獲取到為空 , 可以為該變量或表達式設置一個默認值 ;

student?.name??"Tom"

上述代碼的作用是如果 student?.name 值為空 , 則返回 “Tom” 默認值 ;

代碼示例 :

/// 2. 設置默認值/// 使用 ?? 可以為某個空值設置一個默認值/// 這里如果 student?.name 為空 , 則默認值是 "Tom"print("打印 student 名字 : ${student?.name??"Tom"}");

執(zhí)行結果 :

打印 student 名字 : Tom



四、完整代碼示例



完整代碼示例 :

/// 可脫離 Flutter 環(huán)境獨立執(zhí)行的函數(shù) void main(){print("main");Student student;/// 1. 安全調用 : 不確定對象是否為空 , 通過 ?. 的方式調用/// ( 類似于 Kotlin 的可空類型調用 )/// ? 的作用是先判定是否為空 , 如果為空 , 就終止調用print("打印 student 名字 : ${student?.name}");/// 2. 設置默認值/// 使用 ?? 可以為某個空值設置一個默認值/// 這里如果 student?.name 為空 , 則默認值是 "Tom"print("打印 student 名字 : ${student?.name??"Tom"}");}class Student{String name;int age;Student(this.name, this.age); }

代碼執(zhí)行結果 :

main 打印 student 名字 : null 打印 student 名字 : Tom



五、 相關資源



參考資料 :

  • Flutter 官網 : https://flutter.dev/
  • Flutter 開發(fā)文檔 : https://flutter.cn/docs ( 強烈推薦 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社區(qū) : https://flutter.cn/
  • Flutter 實用教程 : https://flutter.cn/docs/cookbook
  • Flutter CodeLab : https://codelabs.flutter-io.cn/
  • Dart 中文文檔 : https://dart.cn/
  • Dart 開發(fā)者官網 : https://api.dart.dev/
  • Flutter 中文網 ( 非官方 , 翻譯的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
  • Flutter 相關問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )

博客源碼下載 :

  • GitHub 地址 : https://github.com/han1202012/flutter_app_hello ( 隨博客進度一直更新 , 有可能沒有本博客的源碼 )

  • 博客源碼快照 : https://download.csdn.net/download/han1202012/15463304( 本篇博客的源碼快照 , 可以找到本博客的源碼 )

總結

以上是生活随笔為你收集整理的【Flutter】Dart 技巧 ( 独立主函数入口 | 可空类型判定 | 默认值设定 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。