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
Error handling in Flutter
Search
Enzo Lizama Paredes
May 22, 2020
Programming
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Error handling in Flutter
Enzo Lizama Paredes
May 22, 2020
More Decks by Enzo Lizama Paredes
See All by Enzo Lizama Paredes
BDD in Flutter
enzoftware
0
93
Adding Flutter to an existing Android/iOS app
enzoftware
0
170
Flutter flavors
enzoftware
0
80
Flutter CI/CD with Fastlane
enzoftware
0
88
Flutter Animations
enzoftware
0
62
Productivity tools 4 developers
enzoftware
0
53
OpenCV + Android
enzoftware
1
70
Anko Superpowers
enzoftware
0
80
Mobile Vision API + Android
enzoftware
0
70
Other Decks in Programming
See All in Programming
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
970
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
210
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.5k
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
7.1k
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.6k
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
Webフレームワークの ベンチマークについて
yusukebe
0
180
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
170
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
14
6.4k
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.8k
Inside Stream API
skrb
1
800
act1-costs.pdf
sumedhbala
0
120
Featured
See All Featured
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
55k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
WCS-LA-2024
lcolladotor
0
660
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
Faster Mobile Websites
deanohume
310
32k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
570
Bash Introduction
62gerente
615
220k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.6k
Leo the Paperboy
mayatellez
7
1.9k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
870
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
Transcript
Enzo Lizama Error handling in Flutter @enzoftware
KotlinConf 2019: Error Handling Strategies for Kotlin Programs by Nat
Pryce & Duncan McGregor What is failure?
None
Programs can go wrong for so many reasons • Invalid
input ◦ Strings with invalid values ◦ Numbers out of range ◦ Unexpectedly null pointers exceptions • External failure ◦ File not found ◦ Timeouts • Programming errors ◦ Array out of bound ◦ Invalid state • System error ◦ Out of memory
https://dart.dev/guides/libraries/library-tour#exceptions Exceptions are considered conditions that you can plan ahead
for and catch. Errors are conditions that you don’t expect or plan for.
https://flutter.dev/docs/testing/errors How Flutter handle errors
None
FlutterError.onError = (FlutterErrorDetails details) { FlutterError.dumpErrorToConsole(details); if (kReleaseMode) { exit(1);
// Report problem and track it } };
/// This is an [FlutterErrorDetails], appears instead of the red
screen /// to avoid scare the users ErrorWidget.builder = (FlutterErrorDetails details) => CustomErrorWidget(); ... class CustomErrorWidget extends StatelessWidget { @override Widget build(BuildContext context) { // Your custom error widget } }
/// This is an [FlutterErrorDetails], appears instead of the red
screen /// to avoid scare the users ErrorWidget.builder = (FlutterErrorDetails details) => CustomErrorWidget(); ... class CustomErrorWidget extends StatelessWidget { @override Widget build(BuildContext context) { // Your custom error widget } }
An example of error handling in Flutter A strategy
class Failure { final String message; final int statusCode; Failure(this.message,
this.statusCode); @override String toString() => "Error $statusCode. $message."; }
Future<List<HotelModel>> getHotels() async { try { final data = await
http.get(_baseUrl + _endPoint); final responseList = json.decode(data.body); return [for (final hotel in responseList) HotelModel.fromJson(hotel)]; } on SocketException { throw Failure("No internet connection", 400); } on HttpException { throw Failure("Not found request", 404); } on FormatException { throw Failure("Invalid JSON format", 666); } catch (e) { throw Failure("Unknown error", 888); } }
Future<List<HotelModel>> getHotels() async { try { final data = await
http.get(_baseUrl + _endPoint); final responseList = json.decode(data.body); return [for (final hotel in responseList) HotelModel.fromJson(hotel)]; } on SocketException { throw Failure("No internet connection", 400); } on HttpException { throw Failure("Not found request", 404); } on FormatException { throw Failure("Invalid JSON format", 666); } catch (e) { throw Failure("Unknown error", 888); } }
void retrieveHotels() async { try { _hotels = await repository.fetchHotels();
} on Failure catch (e) { _failure = e; } notifyListeners(); }
if (hotelBloc.failure != null) { return Center(child: Text(hotelBloc.failure.toString())); } ...
// The other widgets
Catcher Catcher is Flutter plugin which automatically catches error/exceptions and
handle them. Catcher offers mutliple way to handle errors https://pub.dev/packages/catcher
Enzo Lizama • https://github.com/enzoftware/hotel_booking_app • https://www.youtube.com/watch?v=pvYAQNT4o0I • https://flutter.dev/docs/testing/errors Utils resources
Thanks! @enzoftware