首页 > 解决方案 > 在构建期间调用 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);
        }),
      ],
    ));
  }
}

标签: flutterdart

解决方案


这是因为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(
         ...
      );
    }
}

推荐阅读