![](/img/trans.png)
[英]Error: The method 'showSnackBar' isn't defined for the class 'ScaffoldState'
[英]Having a red line showSnackBar and error in when running the program
我在 Android Studio (mac) 上運行我的 flutter 應用程序,但出現此錯誤:在此處輸入圖像描述
我該如何解決? 這是原始代碼:
Future showCopiedToClipboard(BuildContext context, String text) async {
final lang = Language.of(context);
await FlutterClipboard.copy(text);
final content = lang.copiedToClipboard(text);
if (Scaffold.maybeOf(context) != null)
Scaffold.of(context).showSnackBar(SnackBar(
content: content,
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
));
我試圖在谷歌上搜索它,但沒有找到可以幫助我解決這個問題的正確答案。
使用ScaffoldMessenger.of(context)
而不是Scaffold.of(context)
像這樣:
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: content,
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.