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
スタッフエンジニアの道: The Staff Engineer’s Path
snoozer05
PRO
44
14k
LINEヤフーのフロントエンド組織・体制の紹介
lycorp_recruit_jp
1
1.2k
アプリをリリースできる状態に保ったまま 段階的にリファクタリングするための 戦略と戦術 / Strategies and tactics for incremental refactoring
yanzm
6
1.4k
実務における脅威モデリングを考えよう
nikinusu
0
570
『GRANBLUE FANTASY Relink』キャラクターの魅力を支えるリグ・シミュレーション制作事例
cygames
0
110
サーバレスでモバイルアプリ開発! NTTコム「ビジネスdアプリ」のアーキテクチャ / The architecture of business d app
nttcom
12
240
OSTという文化を組織に根付かせてみた
sansantech
PRO
2
360
AWS SAW を広めたい @四国クラウドお遍路
kazzpapa3
0
230
テスト”ケース”駆動開発 で手戻りをなくそう
ryohma0510
0
310
ロリポップ! for Gamersを支えるインフラ/lolipop for gamers infrastructure
takumakume
0
130
Fediverse Discovery Providers overview
andypiper
0
170
サーバー管理しないサーバーサービスManaged DevOps Pool
kkamegawa
0
130
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
36
1.7k
How To Stay Up To Date on Web Technology
chriscoyier
786
250k
Learning to Love Humans: Emotional Interface Design
aarron
270
40k
Clear Off the Table
cherdarchuk
91
320k
The Pragmatic Product Professional
lauravandoore
31
6.2k
Statistics for Hackers
jakevdp
794
220k
Unsuck your backbone
ammeep
667
57k
Building an army of robots
kneath
302
42k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
123
18k
Code Reviewing Like a Champion
maltzj
517
39k
Embracing the Ebb and Flow
colly
83
4.4k
RailsConf 2023
tenderlove
28
810
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に入ります!