| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Dart

提供: MyMemoWiki
2020年9月27日 (日) 02:59時点におけるPiroto (トーク | 投稿記録)による版 (→‎Enum)
ナビゲーションに移動 検索に移動

Dart

Flutter |

基本

Install

Windows

Mac

$ brew tap dart-lang/dart
$ brew install dart
  • update
$ brew upgrade dart

DartPad

APIリファレンス

パッケージ

  • Dart標準は、dart:
import 'dart:html';
  • ローカルは相対パス(dart:省略)
  • pubパッケージマネージャ

パッケージから特定のオブジェクトのみimportする

  • import [package] show [objects]
import 'package:google_sign_in/google_sign_in.dart'
  show GoogleSignIn, GoogleSignInAccount;

Hello

  • hello.dart
void main() {
    print("Hello Dart!");
}
  • 実行
>dart hello.dart
Hello Dart!

数値

numのサブクラス

  • int
  • double

真偽

  • bool

文字列

String

補完
  • ${}
print("Hello ${this.name}.");

リテラル

  • 一重、二重引用符
  • 引用符3�つ重ねで複数行

StringBuffer

Runes

  • UTF-32型の文字
  • 絵文字などの表現

コレクション

List

  • []
  • add
  • insert
  • removeAt
  • indexOf
  • sort
  • shuffle
  • forEach
  • every
  • map
  • reduce

Map

  • {}
  • remove
  • addAll
  • forEach
  • containsKey

==

Symbol

  • Dartの構文での演算子や識別子

Functions

  • 関数

Enum

  • 列挙
enum Menu { google_sign_in, firestore_cloud_vision }
    • 値を割り当てるだめの妥協
class Status {
  static const int created = 10;
  static const int deleted = 90;
}

文法

基本

  • セミコロン必須

エントリーポイント

  • void main() もしくは、void main(List<String> args)

コメント

  • //
  • /*...*/
  • /// ドキュメンテーションコメント

変数

var

  • 型推論
  • 初期値null
  • 型宣言

定数

final

  • 再代入不可

const

  • コンパイル時点で評価
オブジェクトの変更禁止
final List<int> list1 = [1, 2, 3];
list1.add(4); // [1, 2, 3, 4]  
List<int> list2 = const [1, 2, 3];
list2.add(4); // エラーが発生

コンストラクタ

class Book {
  String title;
  String author;

  Book();

  Book.fromMapWithTitle({Map<String, dynamic> map, String title}) {
    title = title;
    author = map["author"];
    }

  Book.fromMap(Map<String, dynamic> map) :
        this.fromMapWithTitle(map:map);
}

関数

  • 第1級オブジェクト
  • 宣言

<blockquote>常</blockquote>

int someFunc2(int a, int b) {  
  return a + b; 
}

1行

bool isHoge(String str) => str == "hoge";

引数の指定

someFunc3(a: 1, b: 2, c: 3);

オプション引数

String conc(String a, String b, [String c])

デフォルト引数

int someFunc4(int a, {int b = 2})

無名関数(ラムダ、クロージャ)

list.forEach((item) => print(item));

ブロック

  • 変数のスコープ分離

クロージャ

  • 変数のスコープから外れても利用できる

カスケード表記

  • ..でインスタンスの記述を省略できる
  • メソッドを連続で呼び出すことができる

演算子

型演算子

  • as
  • is
  • is!

条件付きメンバアクセス

  • ?.

構文

try catch

  • 例外チェックは行わない
try {  
} on Exception catch (e) {
} catch (e, s) {
    // sはスタックトレース
} finally{}

ジェネリクス

void hoge(List<T> list){}

オブジェクト指向

キーワード

  • 継承 extends
  • 継承元呼び出し super
  • オーバーライド @overrideアノテーション

クラス

非同期処理

  • asyncパッケージをインポート
import 'dart:async';

言語サンプル

File

Tips

実行時の型を調べる

runtimeType

print(o.runtimeType);