【发布时间】:2021-09-28 17:47:18
【问题描述】:
我正在尝试将 main.dart 中的函数传递给小部件,但出现此错误:
错误:参数类型“Function”不能分配给参数类型“void Function()?”。
- “函数”来自“dart:core”。
FilterScreen 类
import 'package:delimails/widgets/main_drawer.dart'; import 'package:flutter/material.dart';
class FilterScreen extends StatefulWidget { static const routeName = '/filter'; final Function saveFilters;
FilterScreen(this.saveFilters);
@override
_FilterScreenState createState() => _FilterScreenState(); }
class _FilterScreenState extends State<FilterScreen> {
@override Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your favorites'),
actions: [
IconButton(icon : Icon(Icons.save), onPressed: **widget.saveFilters**)
],
),
// main class : class MyApp extends StatefulWidget { // This widget is the root of your application. @override _MyAppState createState() => _MyAppState(); }
class _MyAppState extends State<MyApp> { Map<String, bool> _filters = { 'gluten' : false, 'lactose' : false, 'vegan' : false, 'vegetarian' : false, };
void _setFilters (Map<String, bool> filterData){ print('we are here !'); }
@override Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Colors.cyan[300],
accentColor: Colors.orange[100],
canvasColor: Color.fromRGBO(255, 254, 229, 1),
fontFamily: 'Releway',
textTheme: ThemeData.light().textTheme.copyWith(
bodyText1: TextStyle(color: Color.fromRGBO(20, 51, 51, 1)),
bodyText2: TextStyle(color: Color.fromRGBO(20, 51, 51, 1)),
headline6: TextStyle(
fontSize: 20,
fontFamily: 'RobotoCondensed',
fontWeight: FontWeight.bold,
))),
// home: CategoriesScreen(),
routes: {
'/': (ctx) => TabsScreen(),
'/categories': (ctx) => CategoriesScreen(),
CategoryMealsScreen.routeName: (ctx) => CategoryMealsScreen(),
MealDetailScreen.routeName: (ctx) => MealDetailScreen(),
FilterScreen.routeName : (ctx) => FilterScreen(_setFilters),
},
); } }
【问题讨论】:
标签: flutter