【发布时间】:2021-03-29 14:27:54
【问题描述】:
我用英雄包裹了一个脚手架。从抽屉中打开子页面并显示 SnackBar() 小部件时,使用
ScaffoldMessenger.of(context).showSnackBar(snackBar);
我得到了错误
A Hero widget cannot be the descendant of another Hero widget.
'package:flutter/src/widgets/heroes.dart':
Failed assertion: line 375 pos 7: 'context.findAncestorWidgetOfExactType<Hero>() == null'
找到临时解决问题的解决方案:
将全局键添加到脚手架
final _scaffoldKey = GlobalKey<ScaffoldState>();
并用
调用小吃店_scaffoldKey.currentState..showSnackBar(snackBar); //this is working but deprecated
也试过
ScaffoldMessenger.of(_scaffoldKey.currentContext).showSnackBar(snackBar); //also leads to the error with the Hero
编辑 - 在 GitHub 上提交了一个问题: https://github.com/flutter/flutter/issues/79359
【问题讨论】:
标签: flutter