实际上,我创建了一个包含表单小部件的页面,
addInventoryPage
并且formkey(GlobalKey<FormState>())
有一些业务要求使用具有不同路线的同一页面
(routes1:/inventory,routes2:/openingInventory)
和一些额外的参数。
但是当我从/inventory
路由切换到路由时,/openingInventory
我的 fromKey 为 null
这是 addInventoryPage 的代码
class StateCommonPage extends StatelessWidget {
InventoryTabController _inventoryTabController = Get.find();
VariationController _variationController = Get.find();
CommonWidget _commonWidget = CommonWidget();
ValidationMethods _validationMethods = ValidationMethods();
final description = TextEditingController();
final quantity = TextEditingController();
GlobalKey<FormState> formKey=GlobalKey<FormState>(debugLabel: 'STATE COMMON');
final States titleText;
double width;
double height;
StateCommonPage({this.titleText});
@override
Widget build(BuildContext context) {
width = Get.width * 0.95;
height = Get.height;
return Scaffold(
body: Center(
child: Container(
height: height * 0.6,
child: Form(
key:formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
'Fill Details of ${titleText.toString().split('.').last.capitalizeFirst} '
'Process',
style: Theme.of(context).textTheme.headline2,
),
..........
用于在路线之间切换Get.offNamed('inventory');
这是我的 customDrawer
class CustomDrawer extends StatelessWidget {
Widget buildMenuListTile(BuildContext context, Menus menu) {
return Obx(
() => menu.menuCode == 'INVENTORY'
? custom.ExpansionTile(
initiallyExpanded: true,
children: buildSubMenuTile(context, menu.menuCode),
leading: Icon(
Finals.menuIconMap[menu.menuCode],
size: 24,
color: Theme.of(context).primaryColor,
),
title: Text(
menu.menuName,
style: menu.menuCode == selectedMenuId.value
? Theme.of(context).textTheme.headline4
: Theme.of(context).textTheme.headline3,
),
)
: ListTile(
selectedTileColor: Theme.of(context).accentColor.withOpacity(0.3),
leading: Icon(
Finals.menuIconMap[menu.menuCode],
size: 24,
color: Theme.of(context).primaryColor,
),
selected: menu.menuCode == selectedMenuId.value,
hoverColor: Theme.of(context).primaryColor,
title: Text(
menu.menuName,
style: menu.menuCode == selectedMenuId.value
? Theme.of(context).textTheme.headline4
: Theme.of(context).textTheme.headline3,
),
onTap: () {
Get.offNamed(menu.relativePath);
}),
);
}
}
这是我的路线
class Routes {
static const INVENTORY = '/inventory';
static const OPENING_INVENTORY = '/openingInventory';
Map<String, WidgetBuilder> test;
static final routes = [
GetPage(
name: INVENTORY,
page: () => InventoryBottomTabBar(),
binding: BindingsBuilder.put(() => InventoryTabController()),
),
GetPage(
name: OPENING_INVENTORY,
page: () => InventoryBottomTabBar(),
binding:
BindingsBuilder.put(() => InventoryTabController()),
)
}
调试日志
最终确定小部件树时引发了以下断言:在小部件树中检测到重复的 GlobalKey。
在小部件树中多次指定了以下 GlobalKey。这将导致部件树的某些部分被意外截断,因为第二次看到键时,前一个实例被移动到新位置。关键是:
- [LabeledGlobalKey#0db7f] 这是通过注意到具有上述全局键的小部件从其先前的父级移出后确定的,先前的父级在此帧期间从未更新,这意味着它要么根本没有更新,要么在小部件之前更新被移动了,在任何一种情况下都暗示它仍然认为它应该有一个具有该全局密钥的孩子。由于 GlobalKey reparenting 导致一个或多个孩子被强制移除后没有更新的特定 parent 是:
- ConstrainedBox(BoxConstraints(0.0<=w<=Infinity, h=484.4), renderObject: RenderConstrainedBox#37684 relayoutBoundary=up2) 在部件树中一次只能在一个部件上指定 GlobalKey。抛出异常时,这是堆栈:#0 BuildOwner.finalizeTree。(package:flutter/src/widgets/framework.dart:2900:15) #1 BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2925:8) #2 WidgetsBinding.drawFrame (package:flutter/src /widgets/binding.dart:877:19) #3 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5) #4 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart :1144:15)