DEV Community

Cover image for Exception handling and logging in dart/Flutter - Talker
Stanislav Ilin
Stanislav Ilin

Posted on

Exception handling and logging in dart/Flutter - Talker

Exceptions, Exceptions, Exceptions...
We see them constantly during development and support of Applications.

Some of them we expect, and some we haven't even thought about. The worst thing you can do as a programmer is not to think about catching and tracking errors. Let's figure out how to do it...

Exceptions handling in dart

For example, we have this code. This is a call of logic from some service.

final result = service.makeLogic();
print(result);
Enter fullscreen mode Exit fullscreen mode

What will happen if we get an error in the service?
The answer is simple: program crashes and ends with an error
Image description

Such moments should be avoided. To do this, dart has a try-catch construct.

String? result;
try {
  result = service.makeLogic();
} on Exception catch (e) {
  print(e);
}
result ??= 'Can`t get result';
print(result);
Enter fullscreen mode Exit fullscreen mode

Image description
In this case, the program will be able to continue execution and will terminate correctly

Exceptions Tracking

We've figured out how to catch bugs, but what to do with tracking and error analysis?

There is a Crashlytics service from firebase for Flutter applications.
Image description

It can track errors in your Flutter application.
Image description

Crashlytics, Sentry, AppsFlyer and many others. There are even projects that make their own analytical system.
In startups, these tools can change frequently. Are you want to manually write something like this every time?

  try {
    // your code...
  } on Exception catch (exception, stackTrace) {
    Crashlytics.instance
        .recordError(exception, stackTrace, reason: exception.message);
    Sentry.captureException(exception, stackTrace: stackTrace);
    YourOwnAnalytics.sendError(exception, stackTrace: stackTrace);
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text(exception.toString())));
    Logger.error('Something Service exception', exception, stackTrace);
  }
Enter fullscreen mode Exit fullscreen mode

It looks terrible🀒
But how we can fix this case?

Talker

Image description
I present to you the stable release of my library Talker

Talker is advanced exception handling and logging tool for dart/flutter applications.
It is a system for storing and distributing debugging information of your application.

Get started

You can get started at Talker documentation site

But I'll tell you here too

For Dart applications you need add talker dependency to your pubspec.yaml

dependencies:
  talker: ^1.0.0
Enter fullscreen mode Exit fullscreen mode

For Flutter applications you can add talker_flutter or talker dependency to your pubspec.yaml

dependencies:
  talker_flutter: ^1.0.0
Enter fullscreen mode Exit fullscreen mode

What's difference ?

talker_flutter has advanced features that's convenient to use in the Flutter application
like TalkerScreen or TalkerRouteObserver

talker package working both for dart and flutter applications

Easy to use

All you need for the first start is to import the dependency and create an instance of the Talker class

import 'package:talker/talker.dart';
void main(){
  final talker = Talker();
  talker.info('I`m alive πŸ˜„');
}
Enter fullscreen mode Exit fullscreen mode

You can use Talker instance everywhere in your code

Simple and concise syntax will help you with this

final talker = Talker();
// Handle exceptions and errors
try {
  // your code...
} on Exception catch (e, st) {
  talker.handle(e, st, 'Exception in ...');
}

// Make logs
talker.log('App is started'),
talker.error('App is started'),
talker.waring('App is started'),
Enter fullscreen mode Exit fullscreen mode

More examples you can get there or in GitHub

Flutter initialization

Setup Talker to listen your app uncaught exceptions in runZonedGuarded onError() callback

void main() {
  final talker = Talker();
  runZonedGuarded(
    () => runApp(BaseEample(talker: talker)),
    (Object error, StackTrace stack) {
      talker.handle(error, stack, 'Uncaught app exception');
    },
  );
}
Enter fullscreen mode Exit fullscreen mode

After such initialization, all uncaught errors and exceptions will be sent to Talker for processing

Observers

And now I will tell you how to fix the problematic situation from the beginning of the article

Using the Talker library, you can simplify error handling in several services

And now this terrible spaghetti code is replaced

  try {
    // your code...
  } on Exception catch (exception, stackTrace) {
    Crashlytics.instance
        .recordError(exception, stackTrace, reason: exception.message);
    Sentry.captureException(exception, stackTrace: stackTrace);
    YourOwnAnalytics.sendError(exception, stackTrace: stackTrace);
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text(exception.toString())));
    Logger.error('Something Service exception', exception, stackTrace);
  }
Enter fullscreen mode Exit fullscreen mode

To this ->

try {
  // your code...
} on Exception catch (e, st) {
    talker.handle(e, st, 'Something Service exception');
}
Enter fullscreen mode Exit fullscreen mode

To send analytics to other services, just add an observer

class ExceptionsAnalyticsTalkerObserver extends TalkerObserver {
  ExceptionsAnalyticsTalkerObserver();

  @override
  Function(TalkerException e) get onException => (e) {
        Crashlytics.instance
            .recordError(e.exception, stack: e.stackTrace, reason: e.message);
        Sentry.captureException(e.exception, stackTrace: e.stackTrace);
        YourOwnAnalytics.sendError(e.exception, stackTrace: e.stackTrace);
      };
}
Enter fullscreen mode Exit fullscreen mode

And implement this observer in talker's settings

final talker = Talker(
    observers: [
        ExceptionsAnalyticsTalkerObserver(),
    ],
);
Enter fullscreen mode Exit fullscreen mode

Now all your app exception events will come to ExceptionsAnalyticsTalkerObserver observer and will be redirected to the necessary services
Isn 't it beautiful? 😌

You can read more in Customization topic at Talker documentation site

In addition to the above, Talker can do a lot of other things. Customize logs, share a log report, etc.

I put a lot of effort and time into this package. Develop and test it for 4 months. Now I am implementing it into my existing production projects. It works great and the developers are very happy with it.

❀️I will be very pleased if you promote this project and put a star on GitHub and pub.dev

I will also be very glad if you try it in your project and report any shortcomings. For documentation, visit our personal website

Thank you very much for your attention!!!

Top comments (15)

Collapse
 
themfon profile image
Mfon.

This is brilliant, Stanislav πŸ…

Collapse
 
_echo3d_ profile image
echo3D

The detail! Wow.

Collapse
 
dricomdragon profile image
Jovian Hersemeule

Hello, sorry, not yet a Dart programmer, just reporting a misprint :
talker.warning seems expected instead of talker.waring ... Your library seems to be simple and helpful though. πŸ™‚

Collapse
 
cocoyoon profile image
SoYounJeong

Nice to meet you, Stanislav! Looking forward to getting a great insight !

Collapse
 
frezyx profile image
Stanislav Ilin

Thanks ! It pleases )

Collapse
 
khokon profile image
Khokon M.

Very detailed as always!

Collapse
 
frezyx profile image
Stanislav Ilin • Edited

Thank you so much for comment ! 😌

Collapse
 
af profile image
Andrey Frol

I have just begun my flutter journey, but exception handling is always a pain point for me

Collapse
 
b_plab98 profile image
Biplab Dutta πŸ‡³πŸ‡΅πŸ“±πŸ§‘β€πŸ’»

Nice article Stanislav πŸ‘

Collapse
 
frezyx profile image
Stanislav Ilin

Thank you

Collapse
 
ahmedmansoor012 profile image
Mansoor Ahmed

Hi Stanislav

Please subscribe my YouTube Channel: youtube.com/channel/UCbHQ84DcJ_mZG...

Collapse
 
myxzlpltk profile image
myxzlpltk

Might be the simplest. Congrats πŸŽ‰

Collapse
 
aryank21 profile image
Aryan Kaushik

Awesome buddy!

Collapse
 
frezyx profile image
Stanislav Ilin • Edited

Very nice! Thank you)

Collapse
 
rordevelopernik profile image
rordevelopernik

to the point i must say.
Very helpful many thanks!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.