【发布时间】:2021-11-28 21:54:51
【问题描述】:
我正在尝试将过滤器发送到过滤器屏幕,但出现错误。
参数类型“void Function(Map
)”不能分配给参数类型“Map ”。
主屏幕代码
import 'package:flutter/material.dart';
import 'package:meal_app/dummy.dart';
import 'package:meal_app/screens/bottom_navigation.dart';
import 'package:meal_app/screens/catagory_item_screen.dart';
import 'package:meal_app/screens/categorise_screen.dart';
import 'package:meal_app/screens/filters_screen.dart';
import 'package:meal_app/screens/homescreen.dart';
import 'package:meal_app/screens/meal_item_details.dart';
import 'package:meal_app/widgets/meal.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Map<String, bool> _filter = {
'gluton': false,
'lactose': false,
"vrgan": false,
"vegetarian": false
};
List<Meal> _availablemeal = DUMMY_MEALS;
void _applyfilter(Map<String, bool> filterData) {
setState(() {
_filter = filterData;
_availablemeal = DUMMY_MEALS.where((meals) {
if (_filter['gluton']! && !meals.isGlutenFree) {}
if (_filter['lactose']! && !meals.isLactoseFree) {
return false;
}
if (_filter['vrgan']! && !meals.isVegan) {
return false;
}
if (_filter['vegetarian']! && !meals.isVegetarian) {
return false;
}
return true;
}).toList();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Meal App',
theme: ThemeData(
primarySwatch: Colors.pink,
accentColor: Colors.amber,
canvasColor: Color.fromRGBO(255, 255, 255, 1),
fontFamily: 'Raleway',
textTheme: ThemeData.light().textTheme.copyWith(
bodyText1: TextStyle(
color: Color.fromRGBO(20, 50, 51, 1),
),
bodyText2: TextStyle(color: Color.fromRGBO(20, 50, 51, 1)),
subtitle1: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
fontFamily: "RobotoCondensed"),
),
),
initialRoute: '/',
routes: {
'/': (ctx) => BottomNavigation(),
CatagoryItemScreen.routes: (ctx) => CatagoryItemScreen(_availablemeal),
MealDetails.routs: (ctx) => MealDetails(),
FiltersScreen.routeName: (ctx) => FiltersScreen(_filter, _applyfilter),
},
);
}
}
当我使用 final VoidCallBack 时,它在 main.dart 中没有出现错误,但在 onPressed:widget.applyfilter(selectedfilter); 中出现错误,然后我将 VoidCallBack 替换为 Function(Map) 然后它开始在 main.dart 代码中出现错误。
过滤屏幕
import 'package:flutter/material.dart';
import 'package:meal_app/widgets/drawer.dart';
class FiltersScreen extends StatefulWidget {
final Function(Map) applyfilter;
final Map<String, bool> currentfilter;
FiltersScreen(this.applyfilter, this.currentfilter);
static const routeName = '/filter';
@override
State<FiltersScreen> createState() => _FiltersScreenState();
}
class _FiltersScreenState extends State<FiltersScreen> {
Widget _buildSwitchTile(String title, String subTitle, var currentVal,
Function(bool) nextQuestion) {
return SwitchListTile(
title: Text(title),
subtitle: Text(subTitle),
value: currentVal,
activeColor: Colors.redAccent,
onChanged: nextQuestion,
);
}
var isGlutenFree = false;
var isLactoseFree = false;
var isVegan = false;
var isVegetarian = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Filter Screen"),
actions: [
IconButton(
icon: Icon(Icons.save),
onPressed: () {
final selectedfilter = {
'gluton': isGlutenFree,
'lactose': isLactoseFree,
"vrgan": isVegan,
"vegetarian": isVegetarian
};
widget.applyfilter(selectedfilter);
})
],
),
drawer: MainDrawer(),
body: Column(children: [
Container(
margin: EdgeInsets.all(20),
child: Text(
"Adjust your filters here",
style: TextStyle(fontWeight: FontWeight.w900, fontSize: 26),
),
),
Expanded(
child: ListView(
children: [
_buildSwitchTile(
"Glutan-Free", "only include Glutan-Free meal", isGlutenFree,
(newValue) {
setState(() {
isGlutenFree = newValue;
});
}),
_buildSwitchTile(
"Lactose-Free", "only include Lactose-Free meal", isLactoseFree,
(newValue) {
setState(() {
isLactoseFree = newValue;
});
}),
_buildSwitchTile(
"Vegan-Free", "only include Vegan-Free meal", isVegan,
(newValue) {
setState(() {
isVegan = newValue;
});
}),
_buildSwitchTile("Vegetarian-Free",
"only include Vegetarian-Free meal", isVegetarian, (newValue) {
setState(() {
isVegetarian = newValue;
});
}),
],
))
]),
);
}
}
【问题讨论】:
标签: flutter dart flutter-web dart-null-safety