gpt4 book ai didi

flutter - 参数类型 'void Function(Map)' 无法分配给参数类型 'Map'

转载 作者:行者123 更新时间:2023-12-05 04:43:48 24 4
gpt4 key购买 nike

我正在尝试将过滤器发送到过滤器屏幕但出现错误。

The argument type 'void Function(Map<String, bool>)' can't be assigned to the parameter type 'Map<String, bool>'.

主屏幕代码

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); 中出现错误,然后我替换了 VoidCallBackFunction(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;
});
}),
],
))
]),
);
}
}

最佳答案

您以错误的顺序传递参数。切换它们:

FiltersScreen.routeName: (ctx) => FiltersScreen(_applyfilter, _filter)

同时在过滤器屏幕中定义匹配的 map 类型:

final Function(Map<String, bool>) applyfilter;

(否则在修正参数顺序后你会收到另一条错误信息)

关于flutter - 参数类型 'void Function(Map<String, bool>)' 无法分配给参数类型 'Map<String, bool>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69504330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com