@@ -103,15 +103,21 @@ class UploaderPageState extends State<UploaderPage> {
103
103
});
104
104
log ("VideoId : ${video .videoId }" );
105
105
log ("Title : ${video .title }" );
106
- showSuccessSnackBar (
107
- context, "Video ${video .videoId } uploaded" );
106
+ if (context.mounted) {
107
+ showSuccessSnackBar (
108
+ context, "Video ${video .videoId } uploaded" );
109
+ }
108
110
} on Exception catch (e) {
109
111
log ("Failed to upload video: $e " );
110
- showErrorSnackBar (
111
- context, "Failed to upload video: ${e .message }" );
112
+ if (context.mounted) {
113
+ showErrorSnackBar (
114
+ context, "Failed to upload video: ${e .message }" );
115
+ }
112
116
} catch (e) {
113
117
log ("Failed to upload video: $e " );
114
- showErrorSnackBar (context, "Failed to upload video $e " );
118
+ if (context.mounted) {
119
+ showErrorSnackBar (context, "Failed to upload video $e " );
120
+ }
115
121
}
116
122
}
117
123
},
@@ -147,21 +153,32 @@ class UploaderPageState extends State<UploaderPage> {
147
153
}
148
154
149
155
void showSuccessSnackBar (BuildContext context, String message) {
150
- showSnackBar (context, message, backgroundColor: Colors .green);
156
+ context. showSnackBar (message, backgroundColor: Colors .green);
151
157
}
152
158
153
159
void showErrorSnackBar (BuildContext context, String message) {
154
- showSnackBar (context, message,
160
+ context. showSnackBar (message,
155
161
backgroundColor: Colors .red,
156
162
duration: const Duration (seconds: 60 ),
157
163
showCloseIcon: true );
158
164
}
165
+ }
166
+
167
+ extension ErrorExtension on Exception {
168
+ String get message {
169
+ if (this is PlatformException ) {
170
+ return (this as PlatformException ).message ?? "Unknown error" ;
171
+ }
172
+ return toString ();
173
+ }
174
+ }
159
175
160
- void showSnackBar (BuildContext context, String message,
176
+ extension BuildContextSnachBarExtension on BuildContext {
177
+ void showSnackBar (String message,
161
178
{Color ? backgroundColor,
162
179
Duration duration = const Duration (seconds: 4 ),
163
180
bool showCloseIcon = false }) {
164
- ScaffoldMessenger .of (context ).showSnackBar (
181
+ ScaffoldMessenger .of (this ).showSnackBar (
165
182
SnackBar (
166
183
content: Text (message),
167
184
backgroundColor: backgroundColor,
@@ -171,12 +188,3 @@ class UploaderPageState extends State<UploaderPage> {
171
188
);
172
189
}
173
190
}
174
-
175
- extension ErrorExtension on Exception {
176
- String get message {
177
- if (this is PlatformException ) {
178
- return (this as PlatformException ).message ?? "Unknown error" ;
179
- }
180
- return toString ();
181
- }
182
- }
0 commit comments