Flutter应用启动报错:RenderCustomMultiChildLayoutBox子项需含父数据ID
问题解决:Every child of a RenderCustomMultiChildLayoutBox must have an ID in its parent data
错误根源
- Expanded不能直接作为Scaffold的body:Expanded是Row/Column这类Flex布局的专属子组件,直接嵌套在Scaffold的body中会破坏布局结构逻辑。
- Row子组件未明确空间分配:Row中的
NavigationRail和SignUpPage没有设置空间分配规则,导致NavigationRail内部的CustomMultiChildLayout系统无法识别子组件的布局标识,触发错误。
修正后的代码
Widget build(BuildContext context) { return Scaffold( body: Row( children: [ SafeArea( child: NavigationRail( selectedIndex: 0, onDestinationSelected: (int index) { print('Test'); }, destinations: const [ NavigationRailDestination( icon: Icon(Icons.home), label: Text("Home"), ), NavigationRailDestination( icon: Icon(Icons.home), label: Text("a"), ), ], ), ), // 用Expanded包裹SignUpPage,让它占据Row的剩余空间 const Expanded( child: SignUpPage(), ), ], ), ); }
额外提示
如果SignUpPage内部包含Scaffold组件,建议移除内部的Scaffold——嵌套Scaffold容易引发布局冲突(比如重复的导航栏、背景层),直接用Container或其他基础布局组件作为根节点即可。
内容的提问来源于stack exchange,提问作者username
相关产品推荐
相关产品推荐

