Skip to content

Commit

Permalink
fix(example): fix use_build_context_synchronously warning in the example
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Oct 21, 2024
1 parent 07dc6a2 commit 6f3b1f1
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,21 @@ class UploaderPageState extends State<UploaderPage> {
});
log("VideoId : ${video.videoId}");
log("Title : ${video.title}");
showSuccessSnackBar(
context, "Video ${video.videoId} uploaded");
if (context.mounted) {
showSuccessSnackBar(
context, "Video ${video.videoId} uploaded");
}
} on Exception catch (e) {
log("Failed to upload video: $e");
showErrorSnackBar(
context, "Failed to upload video: ${e.message}");
if (context.mounted) {
showErrorSnackBar(
context, "Failed to upload video: ${e.message}");
}
} catch (e) {
log("Failed to upload video: $e");
showErrorSnackBar(context, "Failed to upload video $e");
if (context.mounted) {
showErrorSnackBar(context, "Failed to upload video $e");
}
}
}
},
Expand Down Expand Up @@ -147,21 +153,32 @@ class UploaderPageState extends State<UploaderPage> {
}

void showSuccessSnackBar(BuildContext context, String message) {
showSnackBar(context, message, backgroundColor: Colors.green);
context.showSnackBar(message, backgroundColor: Colors.green);
}

void showErrorSnackBar(BuildContext context, String message) {
showSnackBar(context, message,
context.showSnackBar(message,
backgroundColor: Colors.red,
duration: const Duration(seconds: 60),
showCloseIcon: true);
}
}

extension ErrorExtension on Exception {
String get message {
if (this is PlatformException) {
return (this as PlatformException).message ?? "Unknown error";
}
return toString();
}
}

void showSnackBar(BuildContext context, String message,
extension BuildContextSnachBarExtension on BuildContext {
void showSnackBar(String message,
{Color? backgroundColor,
Duration duration = const Duration(seconds: 4),
bool showCloseIcon = false}) {
ScaffoldMessenger.of(context).showSnackBar(
ScaffoldMessenger.of(this).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: backgroundColor,
Expand All @@ -171,12 +188,3 @@ class UploaderPageState extends State<UploaderPage> {
);
}
}

extension ErrorExtension on Exception {
String get message {
if (this is PlatformException) {
return (this as PlatformException).message ?? "Unknown error";
}
return toString();
}
}

0 comments on commit 6f3b1f1

Please sign in to comment.