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

MyMemoWiki

「Dart」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
295行目: 295行目:
 
*asyncパッケージをインポート
 
*asyncパッケージをインポート
 
  import 'dart:async';
 
  import 'dart:async';
 +
==拡張メソッド(Extension method)==
 +
*[https://qiita.com/akiakishitai/items/e2fc2e5ae8b7f6abcde4 拡張メソッド]
 
==[[言語]]サンプル==
 
==[[言語]]サンプル==
 
*https://dart.dev/samples
 
*https://dart.dev/samples
301行目: 303行目:
 
----
 
----
 
*https://api.dart.dev/stable/2.5.0/dart-io/File-class.html
 
*https://api.dart.dev/stable/2.5.0/dart-io/File-class.html
 +
 
==Tips==
 
==Tips==
 
===実行時の型を調べる===
 
===実行時の型を調べる===

2021年8月10日 (火) 12:29時点における版

Dart

| Flutter | ブログカテゴリ(Flutter) | Android Studio | Flutter macos | FlutterFire | Flutter 手順 |

言語仕様


基本

Install


Windows


Mac


  1. $ brew tap dart-lang/dart
  2. $ brew install dart
  • update
  1. $ brew upgrade dart

DartPad


APIリファレンス


パッケージ


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

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


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

Hello


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

数値


numのサブクラス


  • int
  • double

真偽


  • bool

文字列


String


補完

  • ${}
  1. 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


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

文法

基本


  • セミコロン必須

エントリーポイント


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

コメント


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

変数


var


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

late


定数


final


  • 再代入不可

const


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

コンストラクタ


  1. class Book {
  2. String title;
  3. String author;
  4.  
  5. Book();
  6.  
  7. Book.fromMapWithTitle({Map<String, dynamic> map, String title}) {
  8. title = title;
  9. author = map["author"];
  10. }
  11.  
  12. Book.fromMap(Map<String, dynamic> map) :
  13. this.fromMapWithTitle(map:map);
  14. }

関数


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

<blockquote>常</blockquote>

  1. int someFunc2(int a, int b) {
  2. return a + b;
  3. }

1行


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

引数の指定


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

オプション引数


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

デフォルト引数


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

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


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

ブロック


  • 変数のスコープ分離

クロージャ


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

カスケード表記


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

演算子


型演算子


  • as
  • is
  • is!

条件付きメンバアクセス


  • ?.

構文


try catch


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

null安全

  • ver2.1.2 から型レベルでnull安全を導入
  1. final String country = null; // nullを代入できない
  2. final String? country = null; // nullを代入できる

Non Nullable Type


  • 変数に ! を付けることで、その変数が NULL ではないことをコンパイラに伝え、安全に使用できる
  1. void main() {
  2. String? country = "USA";
  3. String myCountry = country!; // myCountry は null不許可 country が null の場合はエラーとなる
  4. }

lateの使用


  • lateキーワード は後で初期化される変数を示すために使用できる。
  • 宣言時ではなく、アクセス時に初期化される。
  • 初期化される前の値にアクセスすると、ランタイムエラーが発生
  1. void main() {
  2. late String country; // null不許可
  3. // ここで、print(value) した場合、ランタイムエラー
  4. country = "USA";
  5. }

ジェネリクス

  1. void hoge(List<T> list){}

オブジェクト指向


キーワード


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

クラス


非同期処理

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

拡張メソッド(Extension method)

言語サンプル

File


Tips

実行時の型を調べる


runtimeType

  1. print(o.runtimeType);

ログ


  1. import 'dart:developer';
  2. inspect(myVar);
  1. import 'dart:convert';
  2. print(json.encode(yourMapVariable));