Slide 1

Slide 1 text

Enzo Lizama Error handling in Flutter @enzoftware

Slide 2

Slide 2 text

KotlinConf 2019: Error Handling Strategies for Kotlin Programs by Nat Pryce & Duncan McGregor What is failure?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

https://flutter.dev/docs/testing/errors How Flutter handle errors

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

FlutterError.onError = (FlutterErrorDetails details) { FlutterError.dumpErrorToConsole(details); if (kReleaseMode) { exit(1); // Report problem and track it } };

Slide 9

Slide 9 text

/// 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 } }

Slide 10

Slide 10 text

/// 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 } }

Slide 11

Slide 11 text

An example of error handling in Flutter A strategy

Slide 12

Slide 12 text

class Failure { final String message; final int statusCode; Failure(this.message, this.statusCode); @override String toString() => "Error $statusCode. $message."; }

Slide 13

Slide 13 text

Future> 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); } }

Slide 14

Slide 14 text

Future> 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); } }

Slide 15

Slide 15 text

void retrieveHotels() async { try { _hotels = await repository.fetchHotels(); } on Failure catch (e) { _failure = e; } notifyListeners(); }

Slide 16

Slide 16 text

if (hotelBloc.failure != null) { return Center(child: Text(hotelBloc.failure.toString())); } ... // The other widgets

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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