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
38
Flutterハンズオン 3
Aya Ebata
August 26, 2024
Tweet
Share
More Decks by Aya Ebata
See All by Aya Ebata
Flutterハンズオン 5
aya_ebata
0
37
JEP 480: Structured Concurrency
aya_ebata
0
220
Flutterハンズオン 4
aya_ebata
0
70
Flutterハンズオン 2
aya_ebata
0
49
Flutterハンズオン 1
aya_ebata
0
77
あたらしい もじれつの かきかた
aya_ebata
0
94
社内勉強会vol.3@ごーふぁー荘
aya_ebata
0
720
社内勉強会vol.2@ごーふぁー荘
aya_ebata
1
730
社内勉強会vol.1@ごーふぁー荘
aya_ebata
0
680
Other Decks in Technology
See All in Technology
Dataverseの検索列について
miyakemito
1
160
日経電子版 for Android の技術的課題と取り組み(令和最新版)/android-20250423
nikkei_engineer_recruiting
1
610
バクラクの認証基盤の成長と現在地 / bakuraku-authn-platform
convto
4
880
意思決定を支える検索体験を目指してやってきたこと
hinatades
PRO
0
380
AI駆動で進化する開発プロセス ~クラスメソッドでの実践と成功事例~ / aidd-in-classmethod
tomoki10
1
760
PostgreSQL Log File Mastery: Optimizing Database Performance Through Advanced Log Analysis
shiviyer007
PRO
1
150
SREからゼロイチプロダクト開発へ ー越境する打席の立ち方と期待への応え方ー / Product Engineering Night #8
itkq
2
1.1k
Асинхронная коммуникация в Go: от понятного к душному. Дима Некрасов, Otello, 2ГИС
lamodatech
0
1.6k
「経験の点」の位置を意識したキャリア形成 / Career development with an awareness of the “point of experience” position
pauli
4
130
DjangoCon Europe 2025 Keynote - Django for Data Science
wsvincent
0
370
今日からはじめるプラットフォームエンジニアリング
jacopen
8
1.9k
ドキュメント管理の理想と現実
kazuhe
3
310
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
GraphQLの誤解/rethinking-graphql
sonatard
71
10k
Speed Design
sergeychernyshev
29
920
Making the Leap to Tech Lead
cromwellryan
133
9.2k
Site-Speed That Sticks
csswizardry
6
530
Optimizing for Happiness
mojombo
378
70k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
780
Statistics for Hackers
jakevdp
798
220k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Thoughts on Productivity
jonyablonski
69
4.6k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
105
19k
4 Signs Your Business is Dying
shpigford
183
22k
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に入ります!