Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Flutterハンズオン 3
Search
Aya Ebata
August 26, 2024
Technology
0
7
Flutterハンズオン 3
Aya Ebata
August 26, 2024
Tweet
Share
More Decks by Aya Ebata
See All by Aya Ebata
Flutterハンズオン 5
aya_ebata
0
7
JEP 480: Structured Concurrency
aya_ebata
0
130
Flutterハンズオン 4
aya_ebata
0
15
Flutterハンズオン 2
aya_ebata
0
15
Flutterハンズオン 1
aya_ebata
0
40
あたらしい もじれつの かきかた
aya_ebata
0
77
社内勉強会vol.3@ごーふぁー荘
aya_ebata
0
610
社内勉強会vol.2@ごーふぁー荘
aya_ebata
1
630
社内勉強会vol.1@ごーふぁー荘
aya_ebata
0
590
Other Decks in Technology
See All in Technology
リアルお遍路+SORACOM IoT
ozk009
1
140
Jetpack Compose Modifier 徹底解説 / Jetpack Compose Modifier
wiroha
0
190
AIで変わるテスト自動化:最新ツールの多様なアプローチ/ 20240910 Takahiro Kaneyama
shift_evolve
0
230
不動産売買取引におけるAIの可能性とプロダクトでのAI活用
zabio3
0
270
「認証認可」という体験をデザインする ~Nekko Cloud認証認可基盤計画
logica0419
2
430
プログラム検証入門
riru
6
870
DevRelの始め方
moongift
PRO
1
390
Google CloudのLLM活用の選択肢を広げるVertex AIのパートナーモデル
nayuts
0
130
ロリポップ! for Gamersを支えるインフラ/lolipop for gamers infrastructure
takumakume
0
130
持続可能なソフトウェア開発を支える『GitHub CI/CD実践ガイド』
tmknom
6
1.3k
グイグイ系QAマネージャーの仕事
sadonosake
0
320
Creative UIs with Compose: DroidKaigi 2024
chrishorner
1
550
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
221
8.8k
The Art of Programming - Codeland 2020
erikaheidi
48
13k
Designing for Performance
lara
604
68k
Being A Developer After 40
akosma
84
590k
Gamification - CAS2011
davidbonilla
79
5k
What's in a price? How to price your products and services
michaelherold
242
11k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
227
52k
Debugging Ruby Performance
tmm1
72
12k
Pencils Down: Stop Designing & Start Developing
hursman
119
11k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
0
100
The Cost Of JavaScript in 2023
addyosmani
42
5.7k
Transcript
Flutterハンズオン 3 2024/08/27 社内勉強会 えばた あや
今日話すこと 1. Dartの基礎構文 後半 - クラス関連 - 非同期 - 例外
- ★練習問題
1. Dartの基礎構文 後半
Dartの基礎構文 手元で試しながら学んでいきましょう〜! DartPad https://dartpad.dev/ ご自身で入れたエディタでもOK!
Dartの基礎構文 引き続き、こちらのページに載っている構文を見ていきます! https://dart.dev/language
クラス - プロパティとコンストラクタを持つクラス class Spacecraft { String name; DateTime? launchDate;
Spacecraft(this.name, this.launchDate); }
クラス - getがついているプロパティは読み込み専用のgetter class Spacecraft { String name; DateTime? launchDate;
int? get launchYear => launchDate?.year; // ... }
クラス - コンストラクタに名前をつけることができる class Spacecraft { // ... Spacecraft(this.name, this.launchDate);
Spacecraft.unlaunched(String name) : this(name, null); }
クラス - 以下のようにプロパティにアクセスをする class Spacecraft { // ... void describe()
{ print('Spacecraft: $name'); var launchDate = this.launchDate; // ... } }
クラス - 以下のようにクラスを呼び出す var voyager = Spacecraft('Voyager I', DateTime(1977, 9,
5)); voyager.describe(); var voyager3 = Spacecraft.unlaunched('Voyager III'); voyager3.describe();
列挙型 - シンプルなenumを定義する場合 enum PlanetType { terrestrial, gas, ice }
列挙型 - 列挙型を拡張することもできる enum Planet { mercury(planetType: PlanetType.terrestrial), venus(planetType: PlanetType.terrestrial),
// コンストラクタ const Planet({required this.planetType}); final PlanetType planetType; }
列挙型 - 定数コンストラクタ(const constructor) - コンストラクタにconstをつける - クラスが不変のオブジェクトを生成する場合、これらの オブジェクトをコンパイル時定数にすることができる enum
Planet { // ... const Planet({required this.planetType}); // ... }
列挙型 - getterやメソッドも定義できる ※ isGiantはgetterで読み取り専用 enum Planet { // ...
bool get isGiant => planetType == PlanetType.gas || planetType == PlanetType.ice; }
列挙型 - 列挙型を使用する場合 final yourPlanet = Planet.mercury; if (!yourPlanet.isGiant) {
print('Your planet is not a "giant planet".'); }
継承 - Dartは単一継承 -> 一つしか指定できない class Orbiter extends Spacecraft {
double altitude; Orbiter(super.name, DateTime super.launchDate, this.altitude); }
ミックスイン - 複数指定可能で、コードを再利用する時に使用 mixin Piloted { int astronauts = 1;
void describeCrew() { print('Number of astronauts: $astronauts'); } } class PilotedCraft extends Spacecraft with Piloted {···}
インターフェース interface class Vehicle { void moveForward(int meters) { //
... } }
インターフェース - implementsで作成したインターフェースを指定 class MockVehicle implements Vehicle { @override void
moveForward(int meters) { // ... } }
抽象クラス - 具体的な実装を持っていないクラス - 抽象クラスはimplements、extendsのどちらかでも呼び出せる abstract class Vehicle { void
moveForward(int meters); }
非同期 - Futureクラスを使用する - thenでコールバックを書いていく(JavaScriptっぽい!) -> delayedメソッドの処理が終わったらthenで書かれた処理をする const oneSecond =
Duration(seconds: 1); Future<void> printWithDelay(String message) { return Future.delayed(oneSecond).then((_) { print(message); }); }
非同期 - async/awaitで書くこともできる(これもJavaScriptっぽい!) - コールバック地獄を回避できる const oneSecond = Duration(seconds: 1);
Future<void> printWithDelay(String message) async { await Future.delayed(oneSecond); print(message); }
例外 - throwで例外を投げることができる if (astronauts == 0) { throw StateError('No
astronauts.'); }
例外 - try-catchで例外を処理する try { // 何か例外をthrowする処理 } on IOException
catch (e) { print('Could not describe object: $e'); }
練習問題1 - 以下を満たすクラスを作成しましょう 1. Orderクラスのプロパティにはアイテムと個数を持つ 2. orderNameというgetterを用意する 3. addメソッドを用意して、個数をインクリメントする void
main() { var order = Order('ラーメン', 1); print(order.orderName); // 「ラーメンが1個」を出力 order.add(); print(order.orderName); // 「ラーメンが2個」を出力 }
練習問題1 解答例 class Order { String item; int total; String? get
orderName => '$itemが$total個'; Order(this.item, this.total); void add() { total++; } }
練習問題2 - このソースコードはエラーで落ちます - エラーで落ちないように修正しましょう class Coffee { String _temperature;
void heat() { _temperature = 'hot'; } void chill() { _temperature = 'iced'; } String serve() => _temperature + ' coffee'; }
練習問題2 解答例 class Coffee { late String _temperature; void heat() {
_temperature = 'hot'; } void chill() { _temperature = 'iced'; } String serve() => _temperature + ' coffee'; }
練習問題2 実行例 void main() { var coffee = Coffee(); // late変数なので_temperatureの初期化がないとエラーになる
coffee.heat(); print(coffee.serve()); }
練習問題3 1. 非同期を使用し、ユーザを取得するfindUser関数を作成する 1. findUserは3秒かかることを擬似的に表現するために3秒待つ 2. その後、適当な名前を返却する 2. main関数でfindUserを呼び出し、出力する
練習問題3 解答例 Future<String> findUser() async { await Future.delayed(Duration(seconds: 3)); return
'あや'; } void main() { findUser().then((user) => print(user)); }
まとめ - クラス関連、非同期、例外についての書き方を学んだ - 次回はFlutterに入ります!