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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
94
Adding Flutter to an existing Android/iOS app
enzoftware
0
170
Flutter flavors
enzoftware
0
84
Flutter CI/CD with Fastlane
enzoftware
0
88
Flutter Animations
enzoftware
0
64
Productivity tools 4 developers
enzoftware
0
53
OpenCV + Android
enzoftware
1
71
Anko Superpowers
enzoftware
0
81
Mobile Vision API + Android
enzoftware
0
71
Other Decks in Programming
See All in Programming
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
180
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
470
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
820
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
370
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
1.6k
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
780
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
330
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
120
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
220
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
400
Featured
See All Featured
The Spectacular Lies of Maps
axbom
PRO
1
910
Marketing to machines
jonoalderson
1
5.7k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Thoughts on Productivity
jonyablonski
76
5.3k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
860
Optimising Largest Contentful Paint
csswizardry
37
3.9k
We Have a Design System, Now What?
morganepeng
55
8.3k
How STYLIGHT went responsive
nonsquared
100
6.2k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
380
Testing 201, or: Great Expectations
jmmastey
46
8.2k
The untapped power of vector embeddings
frankvandijk
2
1.8k
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