「Dart」の版間の差分
ナビゲーションに移動
検索に移動
(→null安全) |
(→言語サンプル) |
||
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
- https://dart.dev/get-dart
- install
- $ 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
- 型宣言
late
- https://qiita.com/nukotsuka/items/66236723bf17c4574608
- 遅延初期化
- 宣言後に初期化されるnon-nullable変数の宣言
- lateで修飾した変数は、宣言時にthisにアクセス可能
定数
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{}
null安全
- https://medium.com/flutterworld/flutter-null-safety-5d20012c2441
- https://gaprot.jp/2021/04/27/flutter-sound-null-safety/
- null安全コードラボ
- ver2.1.2 から型レベルでnull安全を導入
- final String country = null; // nullを代入できない
- final String? country = null; // nullを代入できる
Non Nullable Type
- 変数に ! を付けることで、その変数が NULL ではないことをコンパイラに伝え、安全に使用できる
- void main() {
- String? country = "USA";
- String myCountry = country!; // myCountry は null不許可 country が null の場合はエラーとなる
- }
lateの使用
- lateキーワード は後で初期化される変数を示すために使用できる。
- 宣言時ではなく、アクセス時に初期化される。
- 初期化される前の値にアクセスすると、ランタイムエラーが発生
- void main() {
- late String country; // null不許可
- // ここで、print(value) した場合、ランタイムエラー
- country = "USA";
- }
ジェネリクス
- void hoge(List<T> list){}
オブジェクト指向
- https://dart.dev/guides/language/language-tour#classes
- Dartはクラスとミックスインベースのインターフェースによるオブジェクト指向言語。
- すべてのオブジェクトはクラスのインスタンスでObjectの子孫
キーワード
- 継承 extends
- 継承元呼び出し super
- オーバーライド @overrideアノテーション
クラス
非同期処理
- asyncパッケージをインポート
- import 'dart:async';
拡張メソッド(Extension method)
言語サンプル
File
Tips
実行時の型を調べる
runtimeType
- print(o.runtimeType);
ログ
- import 'dart:developer';
- inspect(myVar);
- import 'dart:convert';
- print(json.encode(yourMapVariable));
© 2006 矢木浩人