参数不匹配的闭包调用:函数“TasksList.build.<anonymous closure>.<anonymous closure>.<anonymous closure>”

问题描述 投票:0回答:1

您好,当我尝试运行该应用程序时,我收到此错误,它表示闭包调用参数不匹配:函数“TasksList.build...” 由于这是我第一次使用该提供商,我不知道这是由于消费者还是什么原因......如果有人可以提供帮助,我将非常感激。

这是完整的错误,其中显示了 2 个异常:



======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for TaskData:
setState() or markNeedsBuild() called during build.

This _InheritedProviderScope<TaskData?> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: _InheritedProviderScope<TaskData?>
  value: Instance of 'TaskData'
  listening to value
The widget which was currently being built when the offending call was made was: TaskTile
  dirty
When the exception was thrown, this was the stack: 
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4862:9)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4874:6)
#2      _InheritedProviderScopeElement.markNeedsNotifyDependents (package:provider/src/inherited_provider.dart:577:5)
#3      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:403:24)

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building TaskTile(dirty):
Closure call with mismatched arguments: function 'TasksList.build.<anonymous closure>.<anonymous closure>.<anonymous closure>'
Receiver: Closure: (dynamic) => Null
Tried calling: TasksList.build.<anonymous closure>.<anonymous closure>.<anonymous closure>()
Found: TasksList.build.<anonymous closure>.<anonymous closure>.<anonymous closure>(dynamic) => Null

The relevant error-causing widget was: 
  TaskTile TaskTile:file:///D:/Development/VS%20Code%20flutter/todoey_app/lib/widgets/tasks_list.dart:15:20
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1      _objectNoSuchMethod (dart:core-patch/object_patch.dart:85:9)
#2      TaskTile.build (package:todoey_app/widgets/task_tile.dart:27:36)
#3      StatelessElement.build (package:flutter/src/widgets/framework.dart:5367:49)
#4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5297:15)
#5      Element.rebuild (package:flutter/src/widgets/framework.dart:5016:7)
#6      StatelessElement.update (package:flutter/src/widgets/framework.dart:5373:5)
#7      Element.updateChild (package:flutter/src/widgets/framework.dart:3685:15)
#8      SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6441:14)
#9      Element.updateChild (package:flutter/src/widgets/framework.dart:3685:15)
#10     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6441:14)
#11     Element.updateChild (package:flutter/src/widgets/framework.dart:3685:15)
#12     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5322:16)
#13     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5462:11)

这是TaskList类代码:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todoey_app/widgets/task_tile.dart';
import '../models/task_data.dart';

class TasksList extends StatelessWidget {
  const TasksList({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Consumer<TaskData>(
      builder: (context, taskData, child) {
        return ListView.builder(
          itemBuilder: (context, index) {
            final task = taskData.tasks[index];
            return TaskTile(
              taskTitle: task.name,
              isChecked: task.isDone,
              checkboxCallback: (checkboxState) {
                taskData.updateTask(task);
              },
              longPressCallback: () {
                taskData.deleteTask(task);
              },
            );
          },
          itemCount: taskData.taskCount,
        );
      },
    );
  }
}

这是 TaskTile 类代码:

import 'package:flutter/material.dart';

class TaskTile extends StatelessWidget {
  final bool isChecked;
  final String taskTitle;
  final Function checkboxCallback;
  final Function longPressCallback;

  TaskTile(
      {required this.isChecked,
      required this.taskTitle,
      required this.checkboxCallback,
      required this.longPressCallback});

  @override
  Widget build(BuildContext context) {
    return ListTile(
      onLongPress: longPressCallback(),
      title: Text(
        taskTitle,
        style: TextStyle(
            decoration: isChecked ? TextDecoration.lineThrough : null),
      ),
      trailing: Checkbox(
        activeColor: Colors.lightBlueAccent,
        value: isChecked,
        onChanged: checkboxCallback(),
      ),
    );
  }
}

当我运行我的应用程序时,它给了我这个错误,我无法诊断问题是什么。

flutter dart listview provider consumer
1个回答
0
投票

最终函数....add ()...

final Function() checkboxCallback; 最终函数()longPressCallback;

© www.soinside.com 2019 - 2024. All rights reserved.