【问题标题】:how to fix Error: The argument type 'Function' can't be assigned to the parameter type 'void Function()?'. - 'Function' is from 'dart:core'如何修复错误:无法将参数类型“函数”分配给参数类型“无效函数()?”。 - “功能”来自“飞镖:核心”
【发布时间】: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


    【解决方案1】:

    你的无格式代码很难说。

    但看起来您将变量/参数saveFilters 定义为Function,它可以是任何可能的函数(String Function(BuildContext)List&lt;int&gt; Function(int, int) 等),它与参数@987654325 的预期类型不匹配@ 的 IconButtonvoid Function()VoidCallback)。

    您能否尝试将saveFilters 的类型明确指定为void Function()

    class FilterScreen extends StatefulWidget {
      FilterScreen(this.saveFilters);
    
      static const routeName = '/filter';
      final void Function() saveFilters;
    
      @override
      _FilterScreenState createState() => _FilterScreenState();
    }
    
    // ... The rest of your code
    

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2021-07-09
      • 2021-08-22
      • 2021-11-03
      相关资源
      最近更新 更多