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:
parent
393d71a8bc
commit
7ff98649e9
1 changed files with 4 additions and 4 deletions
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue