英文:
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 '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
得分: 1
从外观上看,实际上需要是:
itemCount: data.tasks.length,
英文:
From the looks of it, it actually needs to be
itemCount: data.tasks.length,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论