Skip to content

Commit 6f3b1f1

Browse files
committed
fix(example): fix use_build_context_synchronously warning in the example
1 parent 07dc6a2 commit 6f3b1f1

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

example/lib/main.dart

+26-18
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,21 @@ class UploaderPageState extends State<UploaderPage> {
103103
});
104104
log("VideoId : ${video.videoId}");
105105
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+
}
108110
} on Exception catch (e) {
109111
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+
}
112116
} catch (e) {
113117
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+
}
115121
}
116122
}
117123
},
@@ -147,21 +153,32 @@ class UploaderPageState extends State<UploaderPage> {
147153
}
148154

149155
void showSuccessSnackBar(BuildContext context, String message) {
150-
showSnackBar(context, message, backgroundColor: Colors.green);
156+
context.showSnackBar(message, backgroundColor: Colors.green);
151157
}
152158

153159
void showErrorSnackBar(BuildContext context, String message) {
154-
showSnackBar(context, message,
160+
context.showSnackBar(message,
155161
backgroundColor: Colors.red,
156162
duration: const Duration(seconds: 60),
157163
showCloseIcon: true);
158164
}
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+
}
159175

160-
void showSnackBar(BuildContext context, String message,
176+
extension BuildContextSnachBarExtension on BuildContext {
177+
void showSnackBar(String message,
161178
{Color? backgroundColor,
162179
Duration duration = const Duration(seconds: 4),
163180
bool showCloseIcon = false}) {
164-
ScaffoldMessenger.of(context).showSnackBar(
181+
ScaffoldMessenger.of(this).showSnackBar(
165182
SnackBar(
166183
content: Text(message),
167184
backgroundColor: backgroundColor,
@@ -171,12 +188,3 @@ class UploaderPageState extends State<UploaderPage> {
171188
);
172189
}
173190
}
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

Comments
 (0)