[英]ScaffoldMessenger throws a hero animation error
如果用戶成功創建項目,我將使用新的 ScaffoldMessenger 來顯示小吃欄。 在顯示快餐欄時,我將應用程序導航到儀表板。 但是一旦它點擊儀表板就有多個英雄在子樹中共享相同的標簽,就會拋出錯誤。 我沒有在儀表板中使用任何 Hero 小部件,我有一個 FloatingActionButton,但它的 hero 參數設置為 null。
示例代碼:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('A SnackBar has been shown.'),
animation: null,
),
);
Navigator.pushReplacementNamed(context, '/dashboard');
導致此錯誤:
The following assertion was thrown during a scheduler callback:
There are multiple heroes that share the same tag within a subtree.
Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must have a unique non-null tag.
In this case, multiple heroes had the following tag: <SnackBar Hero tag - Text("A SnackBar has been shown.")>
Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must have a unique non-null tag.
In this case, multiple heroes had the following tag: <SnackBar Hero tag - Text("A SnackBar has been shown.")>
Here is the subtree for one of the offending heroes: Hero
tag: <SnackBar Hero tag - Text("A SnackBar has been shown.")>
state: _HeroState#7589f
When the exception was thrown, this was the stack
#0 Hero._allHeroesFor.inviteHero.<anonymous closure>
#1 Hero._allHeroesFor.inviteHero
package:flutter/…/widgets/heroes.dart:277
#2 Hero._allHeroesFor.visitor
package:flutter/…/widgets/heroes.dart:296
#3 ComponentElement.visitChildren
package:flutter/…/widgets/framework.dart:4729
#4 Hero._allHeroesFor.visitor
package:flutter/…/widgets/heroes.dart:309
...
我有同樣的問題。 如果您有嵌套的腳手架,就會發生這種情況。 ScaffoldMessenger 想要將 Snackbar 發送到所有 Scaffolds。 要解決此問題,您需要使用 ScaffoldMessenger 包裹您的 Scaffold。 這確保您只有一個 Scaffold 收到 Snackbar。
ScaffoldMessenger(
child: Scaffold(
body: ..
),
)
我遇到了同樣的問題並通過在使用ScaffoldMessenger.of(context).removeCurrentSnackBar()
調用Navigator
之前刪除 SnackBar 來修復它。
使用您的示例代碼看起來像這樣:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('A SnackBar has been shown.'),
animation: null,
),
);
ScaffoldMessenger.of(context).removeCurrentSnackBar();
Navigator.pushReplacementNamed(context, '/dashboard');
這是幫助我的鏈接: https : //flutter.dev/docs/release/break-changes/scaffold-messenger#migration-guide
希望它對你有用
我通過在下一個事件循環迭代中運行調用來解決這個問題:
Future.delayed(const Duration(), () =>
ScaffoldMessenger.of(context).showSnackBar(SnackBar(...)));
有同樣的問題,結果我有一個腳手架小部件在我的子樹中返回另一個腳手架(哎呀)
如果是這樣,那么您的小吃店會在兩個腳手架上彈出,然后啟動轉換會導致錯誤。
@GreenFrog 的答案假設您自己處理導航,如果您在使用Scaffold
小部件的默認后退按鈕行為時遇到此問題,您需要將Scaffold
包裝在WillPopScope
小部件中,這基本上否決對Navigator
請求,此外,通過使用WillPopScope.onWillPop
您基本上可以調用ScaffoldMessenger.of(context).removeCurrentSnackBar();
就在路由彈出之前。
例子:
...
WillPopScope(
onWillPop: () async {
ScaffoldMessenger.of(context).removeCurrentSnackBar();
return true;
},
child: Scaffold(...,
);
所以這顯然是 Flutter 中的一個錯誤。
最好的“官方”解決方法是在下一幀顯示小吃吧:
WidgetsBinding.instance.addPostFrameCallback((_) {
// ... show the culprit SnackBar here.
});
但我們都可以同意,這首先不應該發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.