The getter ‘size’ isn’t defined for the type ‘TaskData’.

huangapple go评论85阅读模式
英文:

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

问题

这是使学习Flutter变得困难的案例之一。
以下是直接来自(代码来源)的示例:

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];

          // 手势检测
          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(
                    task.task,
                    style: TextStyle(
                      decoration:
                      task.completed ? TextDecoration.lineThrough : null,
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                  ),

                  // 切换开关
                  Switch(
                    value: task.completed,
                    onChanged: (c) => data.toggleTask(task),
                  ),
                ],
              ),
            ),
          );
        },
      );
    });
  }
}

但是它会引发不可避免的错误消息:"类型'TaskData'没有定义'length'获取器"。
有没有简单的方法来解决这个问题?

英文:

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

import &#39;package:flutter/material.dart&#39;;
import &#39;package:provider/provider.dart&#39;;
import &#39;package:task_management/model/task_data.dart&#39;;

class TaskList extends StatelessWidget {
@override
Widget build(BuildContext context) {
	return Consumer&lt;TaskData&gt;(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: () =&gt; 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) =&gt; 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

得分: 1

从外观上看,实际上需要是:

    itemCount: data.tasks.length,
英文:

From the looks of it, it actually needs to be

    itemCount: data.tasks.length,

huangapple
  • 本文由 发表于 2023年7月3日 20:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定