@@ -4,17 +4,16 @@ import 'package:example/choose_user_page.dart';
44import 'package:example/home_page.dart' ;
55import 'package:example/localizations.dart' ;
66import 'package:example/splash_screen.dart' ;
7- import 'package:flutter/cupertino.dart' ;
87import 'package:flutter/foundation.dart' ;
98import 'package:flutter/material.dart' ;
109import 'package:flutter/scheduler.dart' ;
1110import 'package:flutter_secure_storage/flutter_secure_storage.dart' ;
11+ import 'package:sentry_flutter/sentry_flutter.dart' ;
1212import 'package:stream_chat_flutter/stream_chat_flutter.dart' ;
1313import 'package:stream_chat_localizations/stream_chat_localizations.dart' ;
1414import 'package:stream_chat_persistence/stream_chat_persistence.dart' ;
1515import 'package:streaming_shared_preferences/streaming_shared_preferences.dart' ;
1616
17- import 'app_config.dart' ;
1817import 'routes/app_routes.dart' ;
1918import 'routes/routes.dart' ;
2019
@@ -23,8 +22,65 @@ final chatPersistentClient = StreamChatPersistenceClient(
2322 connectionMode: ConnectionMode .regular,
2423);
2524
25+ void sampleAppLogHandler (LogRecord record) async {
26+ if (kDebugMode) StreamChatClient .defaultLogHandler (record);
27+
28+ // report errors to sentry
29+ if (record.error != null || record.stackTrace != null ) {
30+ await Sentry .captureException (
31+ record.error,
32+ stackTrace: record.stackTrace,
33+ );
34+ }
35+ }
36+
37+ StreamChatClient buildStreamChatClient (
38+ String apiKey, {
39+ Level logLevel = Level .SEVERE ,
40+ }) {
41+ return StreamChatClient (
42+ apiKey,
43+ logLevel: logLevel,
44+ logHandlerFunction: sampleAppLogHandler,
45+ )..chatPersistenceClient = chatPersistentClient;
46+ }
47+
2648void main () async {
27- runApp (MyApp ());
49+ const sentryDsn =
50+ 'https://[email protected] /6352870' ;
51+
52+ /// Captures errors reported by the Flutter framework.
53+ FlutterError .onError = (FlutterErrorDetails details) {
54+ if (kDebugMode) {
55+ // In development mode, simply print to console.
56+ FlutterError .dumpErrorToConsole (details);
57+ } else {
58+ // In production mode, report to the application zone to report to sentry.
59+ Zone .current.handleUncaughtError (details.exception, details.stack! );
60+ }
61+ };
62+
63+ Future <void > _reportError (dynamic error, StackTrace stackTrace) async {
64+ // Print the exception to the console.
65+ if (kDebugMode) {
66+ // Print the full stacktrace in debug mode.
67+ print (stackTrace);
68+ return ;
69+ } else {
70+ // Send the Exception and Stacktrace to sentry in Production mode.
71+ await Sentry .captureException (error, stackTrace: stackTrace);
72+ }
73+ }
74+
75+ runZonedGuarded (
76+ () async {
77+ await SentryFlutter .init (
78+ (options) => options.dsn = sentryDsn,
79+ );
80+ runApp (MyApp ());
81+ },
82+ _reportError,
83+ );
2884}
2985
3086class MyApp extends StatefulWidget {
@@ -46,10 +102,7 @@ class _MyAppState extends State<MyApp>
46102 token = await secureStorage.read (key: kStreamToken);
47103 }
48104
49- final client = StreamChatClient (
50- apiKey ?? kDefaultStreamApiKey,
51- logLevel: Level .SEVERE ,
52- )..chatPersistenceClient = chatPersistentClient;
105+ final client = buildStreamChatClient (apiKey ?? kStreamApiKey);
53106
54107 if (userId != null && token != null ) {
55108 await client.connectUser (
0 commit comments