美文网首页Flutter实战专题
Flutter基础篇之四-Dart语法简述

Flutter基础篇之四-Dart语法简述

作者: Michale_Zuo | 来源:发表于2020-09-22 10:36 被阅读0次

 Google开发的Flutter框架是基于Dart语言,这个语言借鉴了很多其他语言的特性。本文总结一下实际开发中要注意的一些语法,语言详情请见官方文档

变量类型推测:不需要声明特殊的类型
var name = 'Voyager I';
var year = 1977;
函数方法
反参类型    函数名 (入参类型 入参) {
}
Widget constructUI (Widget w) {
}

=> 方法只有一个条件,比较常用语匿名函数当做参数

构造方法
1.同名函数
class StackWidget {
 double x,y;
// this 指向当前实例,类似于iOS中的self;
StackWidget (this.x, this.y);
}

2.自定义构造函数

class StackWidget {
 double x,y;
  StackWidget.point(){
    x = 0;
    y = 0;
  }
}

3.父类没有默认构造函数情况下
 1.构造函数列表

// 先设置x,y的值,在调用构造函数
StackWidget.fromJson(Map<String,double> json): x = json['x'],y = json['y'] {
  
}

 2.父类没有参数的构造函数(注:父类没有无参,无命名的函数要手动调用一个构造函数)
 3.main没有参数的构造函数方法
4.工厂构建方法

注:1.factory不是每次都会创建新的实例 2.不能使用this
factory functionName () {
  return xxx ;
}
支持泛型
List<E> 
List <E extends someBaseClass>// 限定泛型类型
导入库
1.
import '库的名字'
2.导入部分库
import '库的名字' show foo;// 只导入foo模块
import '库的名字' hide foo;// 除了foo模块,其他模块都导入
3.懒加载库 defferred as  ,使用是调用loadLibrary()
import '库的名字' defferred as hello;
Future greet() async {
await hello.loadLibrary();
hello.pringGreeting();
}


异步编程

 在实际开发中,请求网络是需要异步的
1.async await ,await要写在async方法里
2.Future API

可选类型?

?是可以为null类型的语法糖
String? str;表示str可以为null
??设置默认值
String value = nullAbleString ?? "";
解包变量的实际值(如果变量为null,不要使用!)
int? intValue = 2;
int value = intValue!;

相关文章

网友评论

    本文标题:Flutter基础篇之四-Dart语法简述

    本文链接:https://www.haomeiwen.com/subject/cpnmyktx.html