Dart 语言优势
- Dart 既可以支持 JIT(动态编译),也可以支持 AOT(静态编译)。在开发阶段热加载的功能就是基于JIT,打包后的程序是通过AOT编译程序。
// 导入库import 'package:flutter/material.dart';// 程序入口void main() { runApp(const MyApp());}// 无状态组件MyAppclass MyApp extends StatelessWidget { // 构造函数 const MyApp({Key? key}) : super(key: key); // 这里是程序的根节点 @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( // This is the theme of your application. // // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or simply save your changes to "hot reload" in a Flutter IDE). // Notice that the counter didn't reset back to zero; the application // is not restarted. primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); }}
变量声明
var content = 'Dart 语法'; // Declare and initialize a variable.var switchOn = false;var current = 0;String name = "by 小德";int count = 0;dynamic example = 'example';Object index = 100;
常量:final和const
// 当类创建的时候初始化final content = 'Dart 语法'; // 编译时初始化static const bool switchOn = false;
Dart 支持的数据类型
|
含义 |
使用 |
| int |
整数,范围为 -2^63 到 2^63 - 1. |
int x = 1;//没有小数点就是int |
| double |
浮点数,64位 |
double y = 1.1 |