【发布时间】:2021-07-30 23:07:33
【问题描述】:
我在切换到另一个页面(从登录到仪表板页面)时遇到这个问题。
错误:
在小部件树中检测到重复的 GlobalKey。
... 由于 GlobalKey reparenting 导致一个或多个孩子被强制移除后未更新的特定 parent 是:
- 扩展(弹性:1)
代码:
class LoginPage extends StatelessWidget {
final GlobalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();
final FocusNode passFocus = FocusNode();
final FocusNode emailFocus = FocusNode();
@override
Widget build(BuildContext context) {
final authProvider = Provider.of<AuthProvider>(context);
return authProvider.status == Status.Authenticating
? Loading()
: Scaffold(
key: _key,
body: ...
和
class LayoutTemplate extends StatelessWidget {
final GlobalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _key,
body: ...
这是我的导航服务:
class NavigationService {
final GlobalKey<NavigatorState> navigatorKey =
GlobalKey<NavigatorState>();
Future<dynamic> navigateTo(String routeName, {ArgumentsClass arguments}) {
return navigatorKey.currentState.pushNamed(routeName, arguments: arguments);
}
Future<dynamic> globalNavigateTo(String routeName, BuildContext context) {
return Navigator.of(context).pushNamed(routeName);
}
void goBack() {
return navigatorKey.currentState.pop();
}
}
【问题讨论】:
标签: flutter flutter-layout flutter-web