Dart 语言优势

  • Dart 既可以支持 JIT(动态编译),也可以支持 AOT(静态编译)。在开发阶段热加载的功能就是基于JIT,打包后的程序是通过AOT编译程序。
  1. // 导入库
  2. import 'package:flutter/material.dart';
  3. // 程序入口
  4. void main() {
  5. runApp(const MyApp());
  6. }
  7. // 无状态组件MyApp
  8. class MyApp extends StatelessWidget {
  9. // 构造函数
  10. const MyApp({Key? key}) : super(key: key);
  11. // 这里是程序的根节点
  12. @override
  13. Widget build(BuildContext context) {
  14. return MaterialApp(
  15. title: 'Flutter Demo',
  16. theme: ThemeData(
  17. // This is the theme of your application.
  18. //
  19. // Try running your application with "flutter run". You'll see the
  20. // application has a blue toolbar. Then, without quitting the app, try
  21. // changing the primarySwatch below to Colors.green and then invoke
  22. // "hot reload" (press "r" in the console where you ran "flutter run",
  23. // or simply save your changes to "hot reload" in a Flutter IDE).
  24. // Notice that the counter didn't reset back to zero; the application
  25. // is not restarted.
  26. primarySwatch: Colors.blue,
  27. ),
  28. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  29. );
  30. }
  31. }

变量声明

  1. var content = 'Dart 语法'; // Declare and initialize a variable.
  2. var switchOn = false;
  3. var current = 0;
  4. String name = "by 小德";
  5. int count = 0;
  6. dynamic example = 'example';
  7. Object index = 100;

常量:final和const

  1. // 当类创建的时候初始化
  2. final content = 'Dart 语法';
  3. // 编译时初始化
  4. static const bool switchOn = false;

Dart 支持的数据类型

含义 使用
int 整数,范围为 -2^63 到 2^63 - 1. int x = 1;//没有小数点就是int
double 浮点数,64位 double y = 1.1