flutter语法基础dart学习

前言

Flutter是谷歌的移动应用SDK,用于在短时间内在iOS和Android上制作高质量的原生界面。

谷歌使用的是自家的语言dart(谷歌亲儿子,据说是因为Dart项目组就在flutter隔壁而被选上的zzzzzzz)

目前跨平台(移动端)的实现方案分为三种:

技术类型

ui渲染方式

性能

开发效率

动态更新

框架代表

H5+原生

webview渲染

一般

Y

Cordova,Ionic

javascript+原生渲染

原生控件渲染

Y

RN,Weex

自绘ui+原生

调用系统api渲染

flutter高

N

QT,Flutter

ps:所有跨平台实现方案更多的是指ui方面和部分通用功能,针对硬件和手机本身的功能(例如,定制相机,蓝牙功能,binder等 仍然需要原生代码实现),建议选择此种实现方式时,尽量以ui展示为主的项目,不过就算只能ui便利咱们还是可以省掉一大波事情的(毕竟不用写两套ui了)

为什么选择flutter呢?主要是看到了这个

其实对于商用app来说,开发效率和动态更新都不是主要的,用户(金主爸爸)用的舒服才是最主要的(开个玩笑)。

主要还是dart的开发模式更接近java,对于常年用java的程序员来说,js实在是习惯不起来。现在开始吧!

基本语法部分

flutter的基础语法是dart语法,就和android的基础语法是java一样,那我们就拿java和dart的语法做个对比,方便我们深入的理解flutter(dart)的语法体系

dart

java

基本类型

dart语言中一切皆对象,没有java中对基本类型的自动装箱和拆箱(var,const)

默认值是null

byte(8位),short(16位),int(32位),long(64位),float(32位),double(64位),

boolean(1位),char(16位)

默认值是0,false,或者null

内置类型

numbers:包含 int,double(var x=1; var hex='1.1111')

strings(var str='aaa')

booleans(bool)

lists(var list=[1,2,3])

maps(var maps={'key1':'value'})

runes(代表 UTF-32 code poinys)

symbols(在标识符前面加一个#符号 #bar)

没有初始化的变量会赋值为null

包装对象类型:Byte

Short,

Integer,

Long,

Float,

Double,

Boolean,

Character

String

List

Map

Set

没有初始化的对象会赋值为null

流程控制语句

if and else

for loops

while and do-while loops

break and continue

switch and case

if()else{}

for(int i=0;i<length;i++){}

switch(){case 1: break; default break;}

while(){}

do{}while()

权限控制

公共的(不写)

_a(带下划线的表示私有)

private

protected

同一包下(不写)

public

操作符

可覆写操作符

不可以

空指针保护

?.(p?.x=4 如果p是null就不赋值)

只能判定不为null(p!=null)

数组

没有,直接就是集合起步

a[]

方法

function(也是var支持的一种类型),支持表达式写法=>,支持返回值,

var a=function(){}

方法可以直接接收方法传参 function(function param1)

匿名方法(){}

参数不可以传入方法

支持lambda表达式()->{}

匿名方法(){}

支持闭包

程序入口

void main(){}

public static void main(String[] args){}

一切皆对象,类型,方法,类

预定义类(例如 Date,String等spi)

自定义类(没有继承,写extends,则自动继承Objec)

基本类型不是类,但是会自动装箱和拆箱

预定义类(例如 Date,String等spi)

自定义类(没有继承,写extends,则自动继承Objec)

mixin模式

内部类

没有

匿名内部类

局部内部类

成员内部类

静态内部类

接口

隐式接口,每个类都隐式定义了一个接口

通过implements实现

需要显示定义接口(interface)

通过implements实现

抽象类

abstract关键字修饰类,内部的方法 不实现就是抽象方法

调用一个没有实现的方法会报异常

abstract修饰类,abstract修饰内部不实现方法

包结构

dart:core是dart默认存在的库

导包:

import 'dart:html';

import 'package:test/test.dart';

加载库中的foo

import 'package:lib1/lib1.dart' show foo;

除了foo其他全加载

import 'package:lib2/lib2.dart' hide foo;

同名导包存在冲突,指定库前缀,增加 as 别名;

延迟库加载 deferred as;

package

java默认导包是java.lang

类路径 包+类文件名

导入其他包 import java.util.Formatter;

泛型

var names = new List<String>();

意味着泛型在运行时携带着类型信息

参数类型约束

List<String> list=new ArrayList();

意味着泛型在运行时携带着类型信息

枚举

使用enum来定义 enum color

获取枚举数组 color.values;

color.

使用enum定义 enum color

获取枚举数组 color.values;

color.

反射和元数据

元数据注解是以@字符开头

导包 dart:mirrors

通过反射获取类的构造方法,方法及属性

java.lang.reflect

注解是以@字符开头

通过反射获取类的构造方法,方法及属性Class Field

创建类对象 newInstance

设置类的属性值

注释

//

/** */

//

/*

*

*/

方法注释

@param 参数描述

@return 返回值

@throws 异常描述 等注释

基本语法库

其中 dart:core 库是Dart 语言初始已经包含的库。其它的任何包在使用前都需要加上import 语句。

导入其他库则需要导入,例如 import 'dart:html';

dart:async

Support for asynchronous programming, with classes such as Future and Stream.

异步编程支持,提供的Future 和 Stream 类。

dart:collection

Classes and utilities that supplement the collection support in dart:core.

对dart:core 提供更多的集合支持

dart:convert

Encoders and decoders for converting between different data representations, including JSON and UTF-8.

不同类型(JSON,UTF-8)间的字符编码,解码支持。

dart:core

Built-in types, collections, and other core functionality for every Dart program.

Dart语言内建的类型,对象以及dart 语言核心的功能。

dart:developer

Interact with developer tools such as the debugger and inspector.

开发者交互工具,包括debugging工具和代码检查工具。

dart:html

HTML elements and other resources for web-based applications that need to interact with the browser and the DOM (Document Object Model).

HTML 元素与浏览器交互。

dart:indexed_db

A client-side key-value store with support for indexes.

保存用于支持‘ client-side key-value’ 的数据。

dart:io

File, socket, HTTP, and other I/O support for server applications.

服务器端文件,socket, HTTP 及其它I/O 操作支持。

dart:isolate

Concurrent programming using isolates: independent workers that are similar to threads but don't share memory, communicating only via messages.

用于隔离不同线程间的数据。

dart:js

Support for interoperating with JavaScript.

与JavaScript 语言间交互的接口。

dart:js_util

Utility methods to efficiently manipulate typed JSInterop objects in cases where the name to call is not known at runtime. You should only use these methods when the same effect cannot be achieved with @JS annotations. These methods would be extension methods on JSObject if Dart supported extension methods.

用于处理JavaScript语言中的‘JSInterop objects’,只有在使用'JSInterop objects'无法实现的时候才使用。

dart:math

Mathematical constants and functions, plus a random number generator.

数字常量及函数,提供随机数算法。

dart:mirrors

Basic reflection in Dart, with support for introspection and dynamic invocation.

Dart语言的反射功能支持,包括自反和动态调用。

dart:svg

Scalable Vector Graphics: Two-dimensional vector graphics with support for events and animation.

事件和动画的矢量图像支持。

dart:typed_data

Lists that efficiently handle fixed sized data (for example, unsigned 8 byte integers) and SIMD numeric types.

指定数据长度的数据处理。

dart:web_audio

High-fidelity audio programming in the browser.

浏览器中高保真音频支持。

dart:web_gl

3D programming in the browser.

浏览器中3D支持。

dart:web_sql

An API for storing data in the browser that can be queried with SQL.

浏览器中SQL查询支持。

ps: flutter中禁止开发者使用mirrors,无法进行反射(flutter允悲)

总结

dart语言和java还是有非常多相似的地方,习惯java语言的开发也会更习惯flutter的写法

了解了dart的基本语法就可以正式开始flutter学习了。

发布了58 篇原创文章 · 获赞 10 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_34203714/article/details/90514902