The getter 'size' isn't defined for the type 'TaskData'

51 Views Asked by At

This is one of the cases that makes it is difficult to learn flutter. Here is an example directly from (code source):

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:task_management/model/task_data.dart';

class TaskList extends StatelessWidget {
@override
Widget build(BuildContext context) {
    return Consumer<TaskData>(builder: (context, data, child) {
    return ListView.builder(
        scrollDirection: Axis.vertical,
        shrinkWrap: true,
        itemCount: data.size,
        itemBuilder: (context, index) {
        final task = data.tasks[index];
            
        // gesture detection
        return GestureDetector(
            onLongPress: () => data.removeTask(task),
            child: Container(
            margin: EdgeInsets.only(bottom: 10),
            padding: EdgeInsets.fromLTRB(12, 5, 8, 5),
            width: double.infinity,
            decoration: BoxDecoration(
                color: Colors.black12,
                borderRadius: BorderRadius.circular(8)),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                    
                // text field
                Text(
                    task.task,
                    style: TextStyle(
                        decoration:
                            task.completed ? TextDecoration.lineThrough : null,
                        fontSize: 16,
                        fontWeight: FontWeight.bold),
                ),
                    
                // switch case
                Switch(
                    value: task.completed,
                    onChanged: (c) => data.toggleTask(task),
                ),
                ],
            ),
            ),
        );
        },
    );
    });
}
}

But it brings up the inevitable error message: "The getter 'length' isn't defined for the type 'TaskData.'" Any easy way to fix this?

1

There are 1 best solutions below

2
Ivo On

From the looks of it, it actually needs to be

    itemCount: data.tasks.length,