Flutter: There are multiple heroes that share the same tag within a subtree異常

今天寫Demo的時候遇到了下面的錯誤:bash

I/flutter: ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter: The following assertion was thrown during a scheduler callback:
I/flutter: There are multiple heroes that share the same tag within a subtree.
I/flutter: Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must
I/flutter: have a unique non-null tag.
I/flutter: In this case, multiple heroes had the following tag: <default FloatingActionButton tag>
I/flutter: Here is the subtree for one of the offending heroes:
I/flutter: # Hero(tag: <default FloatingActionButton tag>, state: _HeroState#c0b28)
...
複製代碼

大體意思是說:ui

在程序回調的的時候發生瞭如下斷言:this

在子樹中有多個Hero 對象共用了一個tag,每一個Hero對象的tag必須是唯一的。spa

而且指出了是FloatingActionButton中的tag 有衝突。3d

之前都沒注意到這個東西,來看一下FloatingActionButton 中的 tag吧。 code

在這裏插入圖片描述
在這裏插入圖片描述
能夠看到 heroTag 有個默認值: <default FloatingActionButton tag>

而恰好個人頁面中有兩個 FloatingActionButton,因此就致使heroTag 衝突了。cdn

解決辦法很簡單,給FloatingActionButton 的heroTag 賦一個新的值。

在這裏插入圖片描述
這樣就能夠正常使用了。

歸根到底仍是 Hero對象中的tag必須惟一,從下面報錯的地方就能夠看到了: 對象

在這裏插入圖片描述
因此相似報錯均可以直接找到有使用Hero對象的地方就好了。
相關文章
相關標籤/搜索