我是 flutter 新手,我仍在学习基础知识,但我遇到了这个异常:setState() 或 markNeedsBuild() 和异常:失败的断言:第 4134 行 pos 12:'!_debugLocked':不是真的 //第一个代码是主文件,第二个是抽屉文件,其中任何一个都没有错误
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Map<String,bool> _filters = {
'gluten': false,'lactose': false,'vegan': false,'vegetarian': false,};
List<Meal> _availableMeals = DUMMY_MEALS;
List<Meal> _favoriteMeals = [];
void _setfilters(Map<String,bool> filterData) {
if (!mounted) return;
setState(() {
_filters = filterData;
_availableMeals = DUMMY_MEALS.where((meal) {
if (_filters['gluten'] == true && !meal.isGlutenFree) {
return false;
}
if (_filters['lactose'] = true && !meal.isLactoseFree) {
return false;
}
if (_filters['vegan'] == true && !meal.isVegan) {
return false;
}
if (_filters['vegetarian'] == true && !meal.isVegetarian) {
return false;
}
return true;
}).toList();
});
}
void _toggleFavorite(String mealId) {
final existingIndex =
_favoriteMeals.indexWhere((meal) => meal.id == mealId);
if (existingIndex >= 0) {
if (!mounted) return;
setState(() {
_favoriteMeals.removeAt(existingIndex);
});
} else {
if (!mounted) return;
setState(() {
_favoriteMeals.add(
DUMMY_MEALS.firstWhere((meal) => meal.id == mealId),);
});
}
}
bool _isMealFavorite(String id) {
return _favoriteMeals.any((meal) => meal.id == id);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'DeliMeals',theme: ThemeData(
primarySwatch: Colors.pink,accentColor: Colors.amber,canvasColor: Color.fromRGBO(255,254,229,1),fontFamily: 'Raleway',textTheme: ThemeData.light().textTheme.copyWith(
bodyText2: TextStyle(
color: Color.fromRGBO(20,51,),bodyText1: TextStyle(
color: Color.fromRGBO(20,headline6: TextStyle(
fontSize: 20,fontFamily: 'RobotoCondensed',fontWeight: FontWeight.bold,)),// home: CategoriesScreen(),initialRoute: '/',// default is '/'
routes: {
'/': (ctx) => TabsScreen(_favoriteMeals),CategoryMealsScreen.routeName: (ctx) =>
CategoryMealsScreen(_availableMeals),MealDetailScreen.routeName: (ctx) =>
MealDetailScreen(_toggleFavorite,_isMealFavorite),FiltersScreen.routeName: (ctx) => FiltersScreen(_filters,_setfilters),},onGenerateRoute: (settings) {
print(settings.arguments);
// if (settings.name == '/meal-detail') {
// return ...;
// } else if (settings.name == '/something-else') {
// return ...;
// }
// return MaterialPageRoute(builder: (ctx) => CategoriesScreen(),);
},onUnknownRoute: (settings) {
return MaterialPageRoute(
builder: (ctx) => CategoriesScreen(),);
}
} ```
class MainDrawer extends StatelessWidget {
Widget buildListTile(String title,IconData icon,Function tapHandler) {
return ListTile(
leading: Icon(
icon,size: 26,title: Text(
title,style: TextStyle(
fontFamily: 'RobotoCondensed',fontSize: 24,onTap: tapHandler(),);
}
@override
Widget build(BuildContext context) {
return Drawer(
child: Column(
children: <Widget>[
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).pushReplacementNamed('/');
}),buildListTile('Filters',Icons.settings,() {
Navigator.of(context).pushReplacementNamed(FiltersScreen.routeName);
}),],);
}
}
Error:
exception:Failed assertion: line 4134 pos 12: '!_debugLocked': is not true