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
47
Flutterハンズオン 3
Aya Ebata
August 26, 2024
Tweet
Share
More Decks by Aya Ebata
See All by Aya Ebata
Flutterハンズオン 5
aya_ebata
0
49
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
85
あたらしい もじれつの かきかた
aya_ebata
0
98
社内勉強会vol.3@ごーふぁー荘
aya_ebata
0
740
社内勉強会vol.2@ごーふぁー荘
aya_ebata
1
750
社内勉強会vol.1@ごーふぁー荘
aya_ebata
0
690
Other Decks in Technology
See All in Technology
Observability в PHP без боли. Олег Мифле, тимлид Altenar
lamodatech
0
340
生成AI時代の開発組織・技術・プロセス 〜 ログラスの挑戦と考察 〜
itohiro73
0
140
Postman AI エージェントビルダー最新情報
nagix
0
110
あなたの声を届けよう! 女性エンジニア登壇の意義とアウトプット実践ガイド #wttjp / Call for Your Voice
kondoyuko
4
440
2025-06-26_Lightning_Talk_for_Lightning_Talks
_hashimo2
2
100
Oracle Audit Vault and Database Firewall 20 概要
oracle4engineer
PRO
3
1.7k
強化されたAmazon Location Serviceによる新機能と開発者体験
dayjournal
2
210
AIエージェント最前線! Amazon Bedrock、Amazon Q、そしてMCPを使いこなそう
minorun365
PRO
14
5.1k
Liquid Glass革新とSwiftUI/UIKit進化
fumiyasac0921
0
210
20250625 Snowflake Summit 2025活用事例 レポート / Nowcast Snowflake Summit 2025 Case Study Report
kkuv
1
310
生成AI時代 文字コードを学ぶ意義を見出せるか?
hrsued
1
300
Oracle Cloud Infrastructure:2025年6月度サービス・アップデート
oracle4engineer
PRO
2
240
Featured
See All Featured
Speed Design
sergeychernyshev
32
1k
Testing 201, or: Great Expectations
jmmastey
42
7.5k
Fireside Chat
paigeccino
37
3.5k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Why Our Code Smells
bkeepers
PRO
337
57k
Being A Developer After 40
akosma
90
590k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Navigating Team Friction
lara
187
15k
Facilitating Awesome Meetings
lara
54
6.4k
Music & Morning Musume
bryan
46
6.6k
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に入ります!