我制作的膳食应用程序使用扑动和本地数据从虚拟数据
链接github项目https://github.com/rowanibrahem/meal.git
我想让过滤器用户选择的膳食成分并保存它,并使过滤的膳食只有这个成分但这个错误,我无法处理它
在main.dart
void _setFilters(Map<String, bool> _filterData) {
setState(() {
_filters = _filterData;
_availableMeals = DUMMY_MEALS.where((meal) {
if (_filters['gluten']! && !meal.isGlutenFree) {
return false;
}
if (_filters['lactose']! && !meal.isLactoseFree) {
return false;
}
if (_filters['vegan']! && !meal.isVegan) {
return false;
}
if (_filters['vegetarian']! && !meal.isVegetarian) {
return false;
}
return true;
}).toList();
});
}
/// routes
routes: {
//...
FiltersScreen.routeName: (context) =>
FiltersScreen(_filters as Function() , _setFilters as Map<String, bool>),
},
滤镜页
class FiltersScreen extends StatefulWidget {
static const routeName = '/filters';
final Function() saveFilters;
final Map<String , bool> currentFilters;
FiltersScreen(this.saveFilters, this.currentFilters);
@override
State<FiltersScreen> createState() => _FiltersScreenState();
}
当您使用位置参数时,它可能会令人困惑,更好的做法是在方法上使用命名参数。现在让我们继续使用当前的编码风格。
FiltersScreen
按此顺序接受
FiltersScreen(this.saveFilters, this.currentFilters);
但是这里的参数传递顺序错误。
FiltersScreen.routeName: (context) =>
FiltersScreen(_filters as Function() , _setFilters as Map<String, bool>),
您还想从回调中获取项目,这将是
class FiltersScreen extends StatefulWidget {
static const routeName = '/filters';
final Function(Map<String, bool> _filterData) saveFilters;
final Map<String, bool> currentFilters;
const FiltersScreen(this.saveFilters, this.currentFilters);
@override
State<FiltersScreen> createState() => _FiltersScreenState();
}
And on main。飞镖路线,
FiltersScreen.routeName: (context) =>
FiltersScreen(_setFilters, _filters),