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

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
6,074 バイト追加 、 2022年2月16日 (水) 09:20
==[[Dart]]==|[[Flutter]] | [https://www.typea.info/blog/index.php/category/flutter/ ブログカテゴリ(Flutter)] | [[Android Studio]] | [[Flutter macos]] | [[FlutterFire]] | [[Flutter 手順]]|
{{amazon|B00JB3CWZI}}
*https://dart.dev/
===言語仕様===
----
*[https://dart.dev/guides/language/spec 言語仕様]
*[https://dart.dev/guides/language/specifications/DartLangSpec-v2.10.pdf 言語仕様2.1]
*[https://www.cresc.co.jp/tech/java/Google_Dart2/introduction/main_page.html Dart2解説]
 
==[https://dart.dev/guides/language/effective-dart/design#prefer-type-annotating-public-fields-and-top-level-variables-if-the-type-isnt-obvious Effective Dart]==
*[https://dart.dev/guides/language/effective-dart/design#prefer-type-annotating-public-fields-and-top-level-variables-if-the-type-isnt-obvious Effective Dart]
 
==基本==
===Install===
----====[[Windows]]====----
*http://www.gekorm.com/dart-windows/
====[[Mac]]====----
*https://dart.dev/get-dart
*install
$ brew tap dart-lang/dart
$ brew install dart
*update $ brew upgrade dart===DartPad[[Dart]]Pad===----
*https://dartpad.dartlang.org/
===APIリファレンス===
----
*https://api.dart.dev/stable/2.5.0/index.html
===パッケージ===
----*Dart標準は、dart[[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====
----
*https://api.dart.dev/stable/2.5.0/dart-core/String-class.html
=====補完=====
----
*${}
print("Hello ${this.name}.");
====リテラル====
----
*一重、二重引用符
*引用符3�つ重ねで複数行
====StringBuffer====
----
*https://api.dart.dev/stable/2.7.2/dart-core/StringBuffer-class.html
 
====Runes====
----
*UTF-32型の文字
*絵文字などの表現
 
===コレクション===
----
====List====
----
*[]
*add
*map
*reduce
=====連結=====
<pre>
void main() {
var l = [1,2,3];
print(l);
[...l, 4, 5, 6];
print(l);
l = [...l, 4, 5, 6];
print(l);
}
</pre>
*結果
<pre>
[1, 2, 3]
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
</pre>
 
====Map====
----
*{}
*remove
*forEach
*containsKey
 
======
====Symbol====
----*Dartの構文での演算子や識別子[[Dart]]の構文での演算子や識別子====[https://www.cresc.co.jp/tech/java/Google_Dart2/language/functions/functions.html Functions]====----*[https://www.cresc.co.jp/tech/java/Google_Dart2/language/functions/functions.html 関数] *関数は実行可能なアクションの抽象化*関数もオブジェクトでその型はFunction*関数は第1級関数(first-class function)であり、ある関数を別の関数にパラメターとして渡すことも可能 
====Enum====
----
*列挙
enum Menu { google_sign_in, firestore_cloud_vision }
**値を割り当てるだめの妥協
<pre>
class Status {
static const int created = 10;
static const int deleted = 90;
}
</pre>
 
==文法==
===基本===
----
*セミコロン必須
====エントリーポイント ====
----*void main() もしくは、void main(List<&lt;String> &gt; args)
====コメント ====
----
=====単一行=====
*//
 
=====複数行=====
*/*...*/
 
=====ドキュメンテーション=====
*/**または///で始まる
* 角括弧を使ってクラス、メソッド、フィールド、トップ・レベル変数、及びパラメタを参照可能
* 角括弧内の名前はそのドキュメント化されたプログラム要素の構文スコープ内で解決
*[https://dart.dev/guides/language/effective-dart/documentation ドキュメンテーション]
*[https://github.com/dart-lang/dartdoc#dartdoc ドキュメンテーションツール]
 
===変数===
----
====var====
----
*型推論
*初期値null
*型宣言
 
====[https://dart.dev/guides/language/language-tour#late-variables late]====
----
*https://qiita.com/nukotsuka/items/66236723bf17c4574608
*遅延初期化
*宣言後に初期化されるnon-nullable変数の宣言
**lateで修飾した変数は、宣言時にthisにアクセス可能
 
===定数===
----
====final====
----
*再代入不可
====const====
----
*コンパイル時点で評価
:;オブジェクトの変更禁止
final List<&lt;int> &gt; list1 = [1, 2, 3];
list1.add(4); // [1, 2, 3, 4]
List<&lt;int> &gt; list2 = const [1, 2, 3];
list2.add(4); // エラーが発生
===コンストラクタ===
----
*https://dev.classmethod.jp/articles/about_dart_constructors/
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>*関数もオブジェクトでその型はFunction*Dartの関数は第1級関数(first-class function)である。*関数を別の関数にパラメタとして渡すことも可能である.
int someFunc2(int a, int b) {
return a + b;
}
====1行矢印構文(arrow syntax)====----* => exprという構文は{ return expr; }の短縮形<blockquote>式のみが矢印(=>)とセミコロン(;)の間に置かれる。例えば、if文はおけないが、条件式は使える。</blockquote> bool isHoge(String str) => &gt; str == "hoge"; 
====引数の指定====
----
someFunc3(a: 1, b: 2, c: 3);
====オプション引数====
----
String conc(String a, String b, [String c])
====デフォルト引数====
----
int someFunc4(int a, {int b = 2})
====無名関数(ラムダ、クロージャラムダ、[[クロージャ]])====----[[Category:ラムダ]]  list.forEach((item) => &gt; print(item)); 
====ブロック====
----
*変数のスコープ分離
====[[クロージャ]]====----
*変数のスコープから外れても利用できる
====カスケード表記====
----
*..でインスタンスの記述を省略できる
*メソッドを連続で呼び出すことができる
 
===演算子===
----
====型演算子====
----
*as
*is
*is!
====条件付きメンバアクセス====
----
*?.
===アクセッサ===
----
*http://dart-ing.blogspot.com/2012/03/dartgetset.html
*クラスのメンバーにgetとset(いわゆるゲッターとセッター)を定義することができます。ゲッターとセッターで定義されたメンバーは、あたかもメンバー変数と同じようにアクセスできますが、変数への代入だけではなく、ほかの処理を追加することもできます。
<pre>
class Line {
Line(this.start, this.end);
int start, end;
int get length() => end - start;
void set length(int len) {
end = start + len;
}
}
</pre>
===構文===
----
====try catch====
----
*例外チェックは行わない
try {
// sはスタックトレース
} finally{}
 
==null安全==
*https://medium.com/flutterworld/flutter-null-safety-5d20012c2441
*https://gaprot.jp/2021/04/27/flutter-sound-null-safety/
*[https://dart.dev/codelabs/null-safety#map-types null安全コードラボ]
 
*ver2.1.2 から型レベルでnull安全を導入
<pre>
final String country = null; // nullを代入できない
final String? country = null; // nullを代入できる
</pre>
 
===Non Nullable Type===
----
*変数に ! を付けることで、その変数が NULL ではないことをコンパイラに伝え、安全に使用できる
<pre>
void main() {
String? country = "USA";
String myCountry = country!; // myCountry は null不許可 country が null の場合はエラーとなる
}
</pre>
===lateの使用===
----
*lateキーワード は後で初期化される変数を示すために使用できる。
*宣言時ではなく、アクセス時に初期化される。
*初期化される前の値にアクセスすると、ランタイムエラーが発生
<pre>
void main() {
late String country; // null不許可
// ここで、print(value) した場合、ランタイムエラー
country = "USA";
}
</pre>
==ジェネリクス==
void hoge(List<&lt;T> &gt; list){}
==オブジェクト指向==
----
*https://dart.dev/guides/language/language-tour#classes
*Dartはクラスとミックスインベースのインターフェースによるオブジェクト指向言語。[[Dart]]はクラスとミックスインベースのインターフェースによるオブジェクト指向言語。
*すべてのオブジェクトはクラスのインスタンスでObjectの子孫
====キーワード====
----
*継承 extends
*継承元呼び出し super
===クラス===
----
==非同期処理==
*asyncパッケージをインポート
import 'dart:async';
==言語サンプル拡張メソッド(Extension method)==*[https://qiita.com/akiakishitai/items/e2fc2e5ae8b7f6abcde4 拡張メソッド]==[[言語]]サンプル==
*https://dart.dev/samples
===File===
----
*https://api.dart.dev/stable/2.5.0/dart-io/File-class.html
 
==Tips==
===実行時の型を調べる===
----
runtimeType
print(o.runtimeType);
 
===ログ===
----
*https://stackoverflow.com/questions/42440061/how-to-fully-dump-print-variable-to-console-in-the-dart-language
<pre>
import 'dart:developer';
inspect(myVar);
</pre>
 
<pre>
import 'dart:convert';
print(json.encode(yourMapVariable));
</pre>
 
[[category:プログラミング言語]]

案内メニュー