Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在导航中使用带有globalkey的小部件_Flutter - Fatal编程技术网

Flutter 在导航中使用带有globalkey的小部件

Flutter 在导航中使用带有globalkey的小部件,flutter,Flutter,我的“main”小部件必须具有全局密钥。如果我使用名为的按钮或等效按钮导航到它,它会在小部件树中生成关于重复键的异常。我只能pop到这个小部件,但这严重降低了我的导航选项和小部件的可重用性 我已经包含了一个小复制案例的来源,运行应用程序,单击主页上的Login按钮,输入3个登录字符和3个密码字符,然后Login 有什么想法吗?在不使用GlobalKey的情况下进行重新设计是一项重大任务 flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═════

我的“main”小部件必须具有全局密钥。如果我使用名为的
按钮或等效按钮导航到它,它会在小部件树中生成关于重复键的异常。我只能
pop
到这个小部件,但这严重降低了我的导航选项和小部件的可重用性

我已经包含了一个小复制案例的来源,运行应用程序,单击主页上的
Login
按钮,输入3个登录字符和3个密码字符,然后
Login

有什么想法吗?在不使用
GlobalKey
的情况下进行重新设计是一项重大任务

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown while finalizing the widget tree:
flutter: Duplicate GlobalKey detected in widget tree.
flutter: The following GlobalKey was specified multiple times in the widget tree. This will lead to parts of
flutter: the widget tree being truncated unexpectedly, because the second time a key is seen, the previous
flutter: instance is moved to the new location. The key was:
flutter: - [GlobalKey#1c91e navKey]
flutter: This was determined by noticing that after the widget with the above global key was moved out of its
flutter: previous parent, that previous parent never updated during this frame, meaning that it either did
flutter: not update at all or updated before the widget was moved, in either case implying that it still
flutter: thinks that it should have a child with that global key.
flutter: The specific parent that did not update after having one or more children forcibly removed due to
flutter: GlobalKey reparenting is:
flutter: - Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null,
flutter: hintOverrides: null, renderObject: RenderSemanticsAnnotations#147f5 NEEDS-PAINT)
flutter: A GlobalKey can only be specified on one widget at a time in the widget tree.
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      BuildOwner.finalizeTree.<anonymous closure> 
flutter: #1      BuildOwner.finalizeTree 
flutter: #2      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame 
flutter: #3      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback 
flutter: #4      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback 
flutter: #5      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame 
flutter: #6      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame 
flutter: #10     _invoke  (dart:ui/hooks.dart:236:10)
flutter: #11     _drawFrame  (dart:ui/hooks.dart:194:3)
flutter: (elided 3 frames from package dart:async)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: Multiple widgets used the same GlobalKey.
颤振:══╡ WIDGETS库捕获到异常╞═══════════════════════════════════════════════════════════
颤振:在完成小部件树时引发了以下断言:
颤振:在小部件树中检测到重复的GlobalKey。
颤振:在小部件树中多次指定了以下GlobalKey。这将导致部分
颤振:小部件树被意外截断,因为第二次看到一个键时,前一次
颤振:实例移动到新位置。关键是:
颤振:-[GlobalKey#1c91e导航键]
颤振:这是通过注意到具有上述全局键的小部件被移出其应用程序后确定的
颤振:前一个父对象,前一个父对象在此帧期间从未更新过,这意味着它也没有更新过
颤振:根本不更新,或者在小部件移动之前更新,这两种情况都意味着它仍然存在
弗利特:认为它应该有一个拥有全球钥匙的孩子。
颤振:由于以下原因而强制删除一个或多个子项后未更新的特定父项:
颤振:GlobalKey重新租赁为:
flatter:-语义(容器:false,属性:semanticProperties,标签:null,值:null,提示:null,
颤振:hintOverrides:null,renderObject:RenderManticsAnnotations(147f5需要-PAINT)
颤振:在小部件树中,一次只能在一个小部件上指定GlobalKey。
颤振:
颤振:抛出异常时,这是堆栈:
颤振:#0 BuildOwner.finalizeTree。
颤振:#1 BuildOwner.finalizeTree
颤振:#2#WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&ScheduleBinding&PaintingBinding&Semantics Binding&RenderBinding&WidgetsBinding.drawFrame
颤振:#3"WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&ScheduleBinding&PaintingBinding&SemanticsBinding&RenderBinding."handlePersistentFrameCallback."
颤振:#4"WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding."调用远程回调
颤振:#5#WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame
颤振:#6#WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&ScheduleBinding.#手绘框架
颤振:#10(dart:ui/hooks.dart:236:10)
颤振:#11(省道:ui/挂钩.省道:194:3)
颤振:(从包省道中删除3帧:异步)
颤振:════════════════════════════════════════════════════════════════════════════════════════════════════
颤振:引发了另一个异常:多个小部件使用同一个GlobalKey。
导入“包装:颤振/材料.省道”;
类MyKey{
静态最终GlobalKey navKey=GlobalKey(debugLabel:'navKey');
}
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
路线:{
“/”:()=>MyHomePage(键:MyKeys.navKey,标题:“主页”),
“/auth”:()=>MyAuth(),
},
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
扁平按钮(
子项:文本('Login'),
onPressed:()=>Navigator.of(context.pushName)('/auth'),
),
正文(
“您已经按了这么多次按钮:”,
),
正文(
“$”计数器“,
样式:Theme.of(context).textTheme.display1,
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}
类MyAuth扩展StatefulWidget{
@凌驾
_MyAuthState createState()=>\u MyAuthState();
}
类_MyAuthState扩展了状态{
字符串登录;
字符串密码;
final _formKey=GlobalKey();
void _doLogin(){
最终形式=_formKey.currentState;
if(form.validate()){
form.save();
Navigator.of(context.pushNamed('/');
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:Text('Login')),
主体:填充物(
填充:常数边集全部(24.0),
孩子:表格(
键:_formKey,
子:列(
儿童:[
TextFormField(
装饰:输入装饰(