[英]1-setState() or markNeedsBuild() called during build. 2-Failed assertion: line 4165 pos 12: '!_debugLocked': is not true
[英]setState() or markNeedsBuild() called during build…Failed assertion: line 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
遞歸重建。 因此,嘗試將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(
...
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.