flutter - 在构建期间调用 setState() 或 markNeedsBuild()....断言失败:第 4134 行 pos 12: '!_debugLocked': is not true
问题描述
非常新的颤振,我得到了那个类型错误。我建立了简单的膳食应用程序。我不知道解决方案
我添加了简单的 Drawer() ,然后在这条路线中出现错误。我尝试了所有在线解决方案,但从未得到解决方案
当我打开抽屉时突然出现那种类型错误我不知道解决方案。
请给我解决方案
import 'package:flutter/material.dart';
import 'package:recipes/screens/filters_screen.dart';
class MainDrawer extends StatelessWidget {
const MainDrawer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Widget buildListTile(String title, IconData icon, Function tapHandler) {
return ListTile(
leading: Icon(
icon,
size: 26,
),
title: Text(
title,
style: TextStyle(
fontFamily: 'RobotoCondensed',
fontSize: 24,
fontWeight: FontWeight.bold),
),
onTap: tapHandler());
}
return Drawer(
child: Column(
children: [
Container(
height: 120,
width: double.infinity,
padding: EdgeInsets.all(20),
alignment: Alignment.centerLeft,
color: Theme.of(context).accentColor,
child: Text(
'Cooking up!',
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 30,
color: Theme.of(context).primaryColor),
),
),
SizedBox(
height: 20,
),
buildListTile('Meals', Icons.restaurant, () {
Navigator.of(context).pushNamed('/');
}),
buildListTile('Filters', Icons.settings, () {
Navigator.of(context).pushNamed(FilterScreen.routeName);
}),
],
));
}
}
解决方案
这是因为buildListTile
每次您点击菜单项时,您的小部件都会递归重建。所以,尝试buildListTile
从方法中移动你的build
方法。
你的类结构应该是这样的:
class MainDrawer extends StatelessWidget {
const MainDrawer({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Drawer(
....
);
}
Widget buildListTile(String title, IconData icon, Function tapHandler) {
return ListTile(
...
);
}
}
推荐阅读
- ios - 使用 Xcode 12 为 iOS 开发构建 SDL 源
- spring-boot - 如何通过 SpringBoot JPA 在 MySQL 中上传和保存图像
- sql-server - 使用 Apply 方法从 xml 更正准确的元素计数
- node.js - 织物 js 画布导出为 mp4 文件
- javascript - 在表格中打印数组?Javascript / HTML
- c - 当我们在代码中出现错误时,内存中会发生什么?
- python - 无法创建自定义布局对象(python)
- r - 使用等宽系列字体在 R 中渲染图不再显示字符
- microservices - 消费(生成订单服务)消息时客户服务出错
- testing - 如何在不克隆的情况下测试两个结构是否具有相同的值?