Fix unchecked use of BuildContext after await

When using the BuildContext after an asynchronus gap (like when using
await), you always need to check if the context is still mounted before
using it to retrieve objects from the widget tree like with
`ScaffoldMessenger.of(context)`.

Additionally you don't need to use
`.then((value) {})` on a future, if that future is already awaited, you
can simply put any following statements on a new line for better
readability of program flow.
This commit is contained in:
Nutcake 2023-11-11 10:20:55 +01:00
parent 393d71a8bc
commit 7ff98649e9

View file

@ -14,10 +14,10 @@ class MessageText extends StatelessWidget {
Widget build(BuildContext context) {
return GestureDetector(
onLongPress: () async {
await Clipboard.setData(ClipboardData(text: message.content)).then((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Copied to clipboard")));
});
await Clipboard.setData(ClipboardData(text: message.content));
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Copied to clipboard")));
}
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,