Dart基础使用手册
程序入口
在每个app中必须有一个
main()函数作为程序的入口点。
你可以在新建的flutter项目中找到它(main.dart)
void main() => runApp(MyApp())
控制台输出
print('this is a log')
变量
Dart是类型安全的 变量必须是明确声明的或者是系统能够解析的类型
String name = 'hello';
var name = 'hello'
//两种都可以
在Dart中,未初始化的变量的初始值为null
布尔
在Dart中,只有布尔值为"true"才被认为true,0不算
null检查
?.运算符左边为null的情况会直接走左边的调用,类似三目运算符??运算符为在左侧表达式为null时为其设置默认值
函数
add(){
}
String getAString(String output){
return output;
}
注释
// 单行
///
/// 文档注释
/* */ 多行
引用
//引用核心库
import 'dart:async';
import 'dart:math';
//引用外来库
import 'package:angular2/angular2.dart';
//引用文件
import 'path/filename'
类
声明类
class Spacecraft {
String name;
DateTime launchDate;
int launchYear;
// 包含成员变量的构造器
Spacecraft(this.name, this.launchDate) {
launchYear = launchDate?.year;
}
// 命名构造函数
Spacecraft.unlaunched(String name) : this(name, null);
// 方法
void describe() {
print('Spacecraft: $name');
if (launchDate != null) {
int years = new DateTime.now().difference(launchDate).inDays ~/ 365;
print('Launched: $launchYear ($years years ago)');
} else {
print('Unlaunched');
}
}
}
使用类
var voyager = new Spacecraft('Voyager I', new DateTime(1977, 9, 5));
voyager.describe();
var voyager3 = new Spacecraft.unlaunched('Voyager III');
voyager3.describe();
继承
在Dart中也是使用
extends来继承的,同时Dart是单继承
接口
Dart没有
interface关键字。在 Dart 中所有的类都隐含的定义了一个接口。因此你可以使用implement来实现任意的类隐含定义的接口。
抽象类
abstract class Describable {
void describe();
void describeWithEmphasis() {
print('=========');
describe();
print('=========');
}
}
任何继承 Describable 的类都有一各 describeWithEmphasis() 函数,这个函数调用子类的 describe() 函数。
异步编程
Dart使用
Future来表示
使用async和await可以避免回调接口嵌套的问题,让异步代码更加简洁。
Future<Null> printWithDelay(String message) async {
await new Future.delayed(const Duration(seconds: 1));
print(message);
}
错误异常
抛出异常
if (astronauts == 0) {
throw new StateError('No astronauts.');
}
捕获异常
异常可以被捕捉,在异步中也可以
try {
for (var object in flybyObjects) {
var description = await new File('$object.txt').readAsString();
print(description);
}
} on IOException catch (e) {
print('Could not describe object: $e');
} finally {
flybyObjects.clear();
}
Getters 和 Setter
class Spacecraft {
// ...
DateTime launchDate;
int get launchYear => launchDate?.year;
// ...
}
Dart基础使用手册的更多相关文章
- Dart基础学习02--变量及内置类型
Dart基础学习02--变量及内置类型 Dart中的变量 首先看一个变量的定义和赋值 var name = 'Bob'; 在Dart中变量名都是引用,这里的name就是一个指向值为Bob的字符串的引用 ...
- laravel基础操作手册
laravel基础操作手册 1.路由配置 测试配置路由: Route::get('/test', 'TestController@index'); 2.控制器书写 3.模型文件 4.增加扩展类文件 L ...
- Dart:2.通过一个简单程序来理解Dart基础语法
一 . 一个简单的 Dart 程序 // 这是程序执行的入口. main() { var number = 42; // 定义并初始化一个变量. printNumber(number); // 调用一 ...
- Dart 基础重点截取 Dart 2 20180417
官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...
- dart基础计数器
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...
- dart基础语法
.关于 runApp() 上面的实例代码中使用了 runApp() 方法,runApp 方法接收的指定参数类型为 Widget,即: runApp(Widget).在 Flutter 的组件树(wid ...
- Dart基础学习03--方法的使用
1.本文主要讲一下Dart中的方法是怎么定义的,下面先看一个简单的例子: void printNumber(num number) { print('The number is $number.'); ...
- Dart基础学习01--走近Dart
什么是Dart 在Dart的官网上是这样介绍Dart的: Dart is an open-source, scalable programming language, with robust libr ...
- Dart基础
dartpad在线调试 :https://dartpad.dartlang.org 运行需要用墙 vscode执行dart 安装 安装dart插件 下载安装dart 配置环境变量 vscode新建 ...
随机推荐
- div+css做出带三角的弹出框 和箭头
一.三角形 https://blog.csdn.net/Szu_AKer/article/details/51755821 notice:三角的那部分可以用图片作为背景,但是容易出现杂边.所以利用cs ...
- k8s手动安装-1
1.组网master可以使用双网卡,一个外网网卡连接外网,并且做proxy server,一个host-only网卡和node连接. 新版vitualbox配置host-only需要在主机网络管理器中 ...
- spring boot 集成 websocket 实现消息主动推送
spring boot 集成 websocket 实现消息主动 前言 http协议是无状态协议,每次请求都不知道前面发生了什么,而且只可以由浏览器端请求服务器端,而不能由服务器去主动通知浏览器端,是单 ...
- linux系统基础的优化以及常用命令
编辑网卡配置文件 vim /etc/sysconfig/network-scripts/ifcfg-eth0 修改配置参数 ONBOOT= yes启动或者关闭ipsystemctl restart/s ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- Selenium-三种等待方式
在UI自动化测试中,必然会遇到环境不稳定,网络慢的情况,这时如果不做任何处理的话,代码会由于没有找到元素而报错.这时我们就要用到wait,而在Selenium中,我们可以用到一共三种等待,每一种等待都 ...
- c#代码规则,C#程序中元素的命名规范
俩种命名方法 1.Pascal 命名法,第一个字母大写其它字母小写Userid 2.Camel命名法,所有单第一方写大写,其它小写,骆峰命名法,userId C#程序中元素的命名规范项目名:公司名.项 ...
- java中 运算符
我们先讨论 &,&&,| ,|| 这四个运算符 boolean a=true; boolean b=true; boolean c=false; //输出:a为true,b ...
- vim安装bundle和使用
一.准备工作 安装Git(因为下面我们选择的插件管理器需要使用到它)安装其他插件前首先需要选择一个Vim插件管理器,我这里选择的是Vundle,Vundle的工作过程中需要通过Git自动从远程创库同步 ...
- 2017ICPC沈阳网络赛 HDU 6201 -- transaction transaction transaction(树上dp)
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...