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
46
Flutterハンズオン 3
Aya Ebata
August 26, 2024
Tweet
Share
More Decks by Aya Ebata
See All by Aya Ebata
Flutterハンズオン 5
aya_ebata
0
47
JEP 480: Structured Concurrency
aya_ebata
0
240
Flutterハンズオン 4
aya_ebata
0
85
Flutterハンズオン 2
aya_ebata
0
55
Flutterハンズオン 1
aya_ebata
0
84
あたらしい もじれつの かきかた
aya_ebata
0
98
社内勉強会vol.3@ごーふぁー荘
aya_ebata
0
740
社内勉強会vol.2@ごーふぁー荘
aya_ebata
1
740
社内勉強会vol.1@ごーふぁー荘
aya_ebata
0
690
Other Decks in Technology
See All in Technology
CIでのgolangci-lintの実行を約90%削減した話
kazukihayase
0
310
今からでも間に合う! 生成AI「RAG」再入門 / Re-introduction to RAG in Generative AI
hideakiaoyagi
1
190
「どこにある?」の解決。生成AI(RAG)で効率化するガバメントクラウド運用
toru_kubota
2
450
AWS アーキテクチャ作図入門/aws-architecture-diagram-101
ma2shita
24
8.8k
OAuth/OpenID Connectで実現するMCPのセキュアなアクセス管理
kuralab
5
480
IIWレポートからみるID業界で話題のMCP
fujie
0
540
Create a Rails8 responsive app with Gemini and RubyLLM
palladius
0
130
Clineを含めたAIエージェントを 大規模組織に導入し、投資対効果を考える / Introducing AI agents into your organization
i35_267
4
740
讓測試不再 BB! 從 BDD 到 CI/CD, 不靠人力也能 MVP
line_developers_tw
PRO
0
270
ハノーバーメッセ2025座談会.pdf
iotcomjpadmin
0
120
技術職じゃない私がVibe Codingで感じた、AGIが身近になる未来
blueb
0
130
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
7.5k
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
42
2.7k
Why Our Code Smells
bkeepers
PRO
337
57k
How STYLIGHT went responsive
nonsquared
100
5.6k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.7k
Raft: Consensus for Rubyists
vanstee
140
7k
Thoughts on Productivity
jonyablonski
69
4.7k
BBQ
matthewcrist
89
9.7k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
43
2.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.8k
Building an army of robots
kneath
306
45k
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に入ります!