错误:没有为类型“StatefulWidget”定义getter“tasks”,(位于[todoey_flutter] lib\widgets\tasks_list.dart的未定义的getter:30)

rjee0c15  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(114)

我得到了一个错误,通过使用getter.widget.tasks....请看一下下面的图片和代码。

import 'package:flutter/material.dart';
import 'task_tile.dart';
import 'package:todoey_flutter/models/task.dart';
import 'package:todoey_flutter/screens/tasks_screen.dart';

class TaskList extends StatefulWidget {
  TaskList({required this.tasks});

 final List tasks;

 @override
 _TaskListState createState() => _TaskListState();
 }

class _TaskListState extends State {
 @override
Widget build(BuildContext context) {
return ListView.builder(
  itemBuilder: (context, index) {
    return TaskTile(
      taskTitle: widget.tasks[index].name,
      isChecked: widget.tasks[index].isDone,
      checkboxCallback: (checkboxState) {
        setState(() {
          widget.tasks[index].toggleDone();
        });
      },
    );
  },
  itemCount: widget.tasks.length,
);
}

kninwzqo

kninwzqo1#

您需要将想要为其创建状态的小部件类型从有状态小部件传递到状态类:

import 'package:flutter/material.dart';
import 'task_tile.dart';
import 'package:todoey_flutter/models/task.dart';
import 'package:todoey_flutter/screens/tasks_screen.dart';

class TaskList extends StatefulWidget {
  TaskList({required this.tasks});

 final List tasks;

 @override
 _TaskListState createState() => _TaskListState();
 }

class _TaskListState extends State<TaskList> { // ADD <TaskList> 
 @override
Widget build(BuildContext context) {
return ListView.builder(
  itemBuilder: (context, index) {
    return TaskTile(
      taskTitle: widget.tasks[index].name,
      isChecked: widget.tasks[index].isDone,
      checkboxCallback: (checkboxState) {
        setState(() {
          widget.tasks[index].toggleDone();
        });
      },
    );
  },
  itemCount: widget.tasks.length,
);
}

相关问题