ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Dart语言基础 - 变量声明: ``` <pre class="calibre14">``` var name<span class="token">=</span><span class="token4">"张三"</span><span class="token2">;</span> 不指定类型 函数类使用 dynamic name<span class="token">=</span><span class="token4">"11"</span><span class="token2">;</span> 不确定具体类型时候用 String name <span class="token">=</span> <span class="token4">'bob'</span><span class="token2">;</span> 指定具体类型 var name<span class="token2">;</span> 没有赋值情况下<span class="token2">,</span>所有类型都是<span class="token5">null</span> 变量作用域在<span class="token2">{</span><span class="token2">}</span>中确定<span class="token2">,</span>层级越深作用域越小<span class="token2">,</span>函数外的作用域很大 final name<span class="token">=</span><span class="token4">"11"</span><span class="token2">;</span> 只能赋值一次<span class="token2">,</span>内容不会改变 const pi<span class="token">=</span><span class="token3">3.14</span><span class="token2">;</span> 编译时候的常量 ``` ``` - 数据类型,基本类型有数字,字符,布尔 ``` <pre class="calibre14">``` int a<span class="token">=</span><span class="token3">1</span><span class="token2">;</span> 整型 double b<span class="token">=</span><span class="token3">1.01</span> 双精度浮点数 num c<span class="token">=</span><span class="token3">1.1</span> 同时支持整型和浮点 方法或属性 isEven 是否偶数 isOdd 是否奇数 字符串可以是单引号或双引号<span class="token2">,</span>变量可以$name替换<span class="token2">,</span>表达式$<span class="token2">{</span><span class="token2">}</span>替换<span class="token2">,</span>拼接字符串用<span class="token">+</span><span class="token2">,</span>多行使用<span class="token4">''</span>'或<span class="token4">""</span>" String name<span class="token">=</span> <span class="token4">'Hi ${name.toUpperCase()}'</span><span class="token2">;</span> String name<span class="token">=</span><span class="token4">''</span>'你好 世界<span class="token4">''</span>'<span class="token2">;</span> 属性或方法 length 长度 isEmpty是否为空 布尔 只有<span class="token3">true</span> 和<span class="token3">false</span><span class="token2">,</span>只有值为<span class="token3">true</span>才是真其余都是<span class="token3">false</span><span class="token2">;</span> List类型<span class="token2">,</span>有序集合其它语言的数组 var a<span class="token">=</span><span class="token2">[</span><span class="token4">"a"</span><span class="token2">,</span><span class="token4">"b"</span><span class="token2">]</span><span class="token2">;</span> 或者 var a<span class="token">=</span><span class="token5">new</span> <span class="token1">List</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>a<span class="token2">.</span><span class="token1">add</span><span class="token2">(</span><span class="token4">"a"</span><span class="token2">)</span><span class="token2">;</span> Set类型<span class="token2">,</span>无序集合 var gameSet <span class="token">=</span> <span class="token2">{</span><span class="token4">'Halo'</span><span class="token2">,</span> <span class="token4">'God of War'</span><span class="token2">}</span><span class="token2">;</span> var a<span class="token">=</span><span class="token5">new</span> <span class="token1">Set</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> Map类型 var gameMap <span class="token">=</span> <span class="token2">{</span><span class="token4">'microsoft'</span><span class="token2">:</span> <span class="token4">'Halo'</span><span class="token2">,</span> <span class="token4">'sony'</span> <span class="token2">:</span><span class="token4">'God of War'</span><span class="token2">}</span><span class="token2">;</span> gameMap<span class="token2">[</span><span class="token4">"sony"</span><span class="token2">]</span><span class="token">=</span><span class="token4">"1"</span><span class="token2">;</span> ``` ``` - 运算符 ``` <pre class="calibre14">``` a is String 是否是字符串<span class="token2">,</span>判断类型 a is<span class="token">!</span> String 不属于某个类型 name as String 类型转换 var message <span class="token">=</span> input<span class="token">?</span><span class="token">?</span> <span class="token4">'Hello'</span><span class="token2">;</span> 判断input是否<span class="token5">null</span><span class="token2">,</span>不是直接返回input<span class="token2">,</span>否则hello person<span class="token2">.</span><span class="token2">.</span>name <span class="token">=</span> <span class="token4">'bob'</span><span class="token2">.</span><span class="token2">.</span>age <span class="token">=</span> <span class="token3">28</span><span class="token2">;</span> 连续操作<span class="token2">.</span><span class="token2">.</span> var message<span class="token2">;</span> message <span class="token">?</span><span class="token">?</span><span class="token">=</span> <span class="token4">'Hello'</span><span class="token2">;</span> 赋值判断是否为<span class="token5">null</span><span class="token2">,</span>然后赋值 ``` ``` - 定义函数 ``` <pre class="calibre14">``` String <span class="token1">bobSay</span><span class="token2">(</span>String words<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token4">'bob say $words'</span><span class="token2">;</span> <span class="token2">}</span> String <span class="token1">bobSay2</span><span class="token2">(</span>String words<span class="token2">)</span> <span class="token">=</span><span class="token">></span> <span class="token4">'bob say $words'</span><span class="token2">;</span> <span class="token6">//简写</span> String <span class="token1">fn</span><span class="token2">(</span>String a<span class="token2">,</span><span class="token2">[</span>String a<span class="token">=</span><span class="token3">1</span><span class="token2">]</span><span class="token2">)</span><span class="token2">{</span><span class="token2">}</span><span class="token6">//调用 fn(1,2),第二个可选</span> String <span class="token1">fn</span><span class="token2">(</span><span class="token2">{</span>String a<span class="token2">,</span>int b<span class="token">=</span><span class="token3">1</span><span class="token2">}</span><span class="token2">)</span><span class="token2">{</span><span class="token2">}</span> <span class="token6">//调用 fn(a:"aa",b:3)</span> ``` ``` - 类 ``` <pre class="calibre14">``` class <span class="token1">A</span><span class="token2">{</span> static int a<span class="token">=</span><span class="token3">1</span><span class="token2">;</span> static <span class="token1">fn</span><span class="token2">(</span>string w<span class="token2">)</span><span class="token2">{</span><span class="token2">}</span> <span class="token1">a</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> <span class="token2">}</span>和类同名函数为构造函数初始化变量<span class="token2">,</span>不能有<span class="token5">return</span> factory <span class="token1">a</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> <span class="token2">}</span> factory修饰的工厂构造函数必须有<span class="token5">return</span> <span class="token2">}</span> ``` ``` 库 ``` <pre class="calibre16">``` <span class="token6">// 三种形式的库导入</span> import dart<span class="token2">:</span>io<span class="token2">;</span> 自带标准库 import <span class="token4">'../base.dart'</span><span class="token2">;</span> import <span class="token4">'package:test/test.dart'</span><span class="token2">;</span> ``` ```