forked from rollbar/rollbar-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
219 lines (191 loc) · 5.65 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import 'dart:io' show Platform;
import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rollbar_flutter/rollbar.dart' show Rollbar, RollbarFlutter;
import 'package:rollbar_flutter/rollbar.dart' as rollbar;
/// Example Flutter application using rollbar-flutter.
Future<void> main() async {
const config = rollbar.Config(
accessToken: 'YOUR-ROLLBAR-ACCESSTOKEN',
package: 'rollbar_flutter_example');
await RollbarFlutter.run(config, () {
Rollbar.drop(rollbar.Breadcrumb.navigation(
from: 'initialize',
to: 'runApp',
));
Rollbar.info('Rollbar initialized');
runApp(const MyApp());
});
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Rollbar Flutter Example',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const MyHomePage(title: 'Rollbar Flutter Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
static const platform = MethodChannel('com.rollbar.flutter.example/activity');
var _userIsLoggedIn = false;
var _setUserText = 'Set User';
var _batteryLevel = 'Unknown battery level.';
var _faultyMsg = 'No successful invocations yet.';
var _counter = 0;
MyHomePageState();
void batteryLevel() async {
Rollbar.drop(
rollbar.Breadcrumb.widget(
element: 'batteryLevel',
extra: const {'action': 'tapped'},
),
);
String batteryLevel;
try {
final int level = await platform.batteryLevel;
batteryLevel = 'Battery at $level%.';
} on PlatformException catch (e, stackTrace) {
batteryLevel = 'Failed to get battery level.';
Rollbar.drop(
rollbar.Breadcrumb.error(
'Non-fatal PlatformException while getting battery level.'),
);
Rollbar.warn(e, stackTrace);
}
setState(() {
_batteryLevel = batteryLevel;
});
}
void faultyMethod() {
Rollbar.drop(rollbar.Breadcrumb.log('Tapped faultyMethod button'));
Rollbar.warn('This method is about to fail');
platform
.faultyMethod()
.then((message) => setState(() => _faultyMsg = message))
.catchError((e) => log(e), test: (_) => false);
}
void incrementCounter() {
Rollbar.drop(rollbar.Breadcrumb.widget(
element: 'incrementCounter',
extra: const {'action': 'tapped'},
));
setState(() {
if (++_counter % 2 == 0) {
throw ArgumentError('Failed to increment counter');
} else {
Rollbar.drop(
rollbar.Breadcrumb.log('Counter incremented to $_counter'),
);
}
});
}
void asyncFailure() {
_asyncFailure(1).then((n) => log('$n ~/ 0 = ???'));
}
Future<int> _asyncFailure(int num) async {
return Future<int>.delayed(Duration(seconds: num), () => num ~/ 0);
}
void setUser() {
if (_userIsLoggedIn) {
Rollbar.setUser(null);
_userIsLoggedIn = false;
Rollbar.debug('User logged out');
} else {
Rollbar.setUser(const rollbar.User(
id: '123456',
username: 'TheUser',
email: '[email protected]',
));
_userIsLoggedIn = true;
Rollbar.debug('User logged in');
}
setState(() {
_setUserText = _userIsLoggedIn ? 'Unset User' : 'Set User';
});
}
void throwError() {
Rollbar.drop(rollbar.Breadcrumb.log('Tapped throwError button'));
Rollbar.critical('About to throw an error!');
throw StateError('A state error occurred');
}
void crash() {
Rollbar.drop(rollbar.Breadcrumb.navigation(from: 'app', to: 'crash'));
platform.crash();
}
@override
Widget build(BuildContext context) {
final bodyChildren = <Widget>[
ElevatedButton(
onPressed: batteryLevel,
child: const Text('Get Battery Level'),
),
Text(_batteryLevel),
ElevatedButton(
onPressed: faultyMethod,
child: const Text('Call Faulty Method'),
),
Text(_faultyMsg),
if (Platform.isIOS)
ElevatedButton(
onPressed: crash,
child: const Text('Crash application'),
),
const Divider(),
ElevatedButton(
onPressed: setUser,
child: Text(_setUserText),
),
const Divider(),
ElevatedButton(
onPressed: asyncFailure,
child: const Text('Async failure'),
),
ElevatedButton(
onPressed: throwError,
child: const Text('Throw error'),
),
const Divider(),
const Text('Times you have pushed the plus button:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
const Divider(height: 10),
];
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: bodyChildren,
),
),
floatingActionButton: FloatingActionButton(
onPressed: incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
extension _Methods on MethodChannel {
Future<int> get batteryLevel async => await invokeMethod('getBatteryLevel');
Future<String> faultyMethod() async => await invokeMethod('faultyMethod');
Future<Never> crash() async => await invokeMethod('crash');
}