type'列表<字符串>'不是类型'映射<字符串,bool>'共&#



我正试图用Flutter创建一个ToDo应用程序来学习Flutter。

import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const ToDo(title: 'Flutter Demo Home Page'),
);
}
}
class ToDo extends StatefulWidget {
final String title;
const ToDo({Key? key, required this.title}) : super(key: key);
@override
_ToDoState createState() => _ToDoState();
}
class _ToDoState extends State<ToDo> {
final Map<String, bool> products = {
'Tomatoes': false,
'Cheese': false,
'Salade': false,
'Paprika': false,
'Water': false
};
void onSubmit(String title) {
setState(() {
products[title] = false;
});
Navigator.of(context).pop();
}
void newEntry() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: UserInput(
onSubmit: onSubmit,
),
);
});
}
void deleteItem(String key) {
setState(() {
products.remove(key);
});
}
void onChanged(String key) {
setState(() {
products.update(key, (value) => !value);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("TODO App"),
),
body: ListView.builder(
itemCount: products.length,
itemBuilder: (context, i) {
String key = products.keys.elementAt(i);
return TodoItem(
title: key,
done: products[key]!,
onDelete: () => deleteItem(key),
onChanged: () => onChanged(key),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: newEntry,
child: const Icon(Icons.add),
),
);
}
}
class TodoItem extends StatelessWidget {
final String title;
final void Function() onChanged;
final void Function() onDelete;
final bool done;
const TodoItem(
{Key? key,
required this.title,
required this.onDelete,
required this.done,
required this.onChanged})
: super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 22),
child: ListTile(
contentPadding: const EdgeInsets.symmetric(vertical: 8.0),
leading: Checkbox(
value: done,
onChanged: (value) => onChanged,
),
title: Text(
title,
style: const TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.w600,
color: Colors.black54),
),
trailing: IconButton(
icon: const Icon(Icons.delete_outline),
onPressed: onDelete,
),
));
}
}
class UserInput extends StatefulWidget {
final void Function(String) onSubmit;
const UserInput({Key? key, required this.onSubmit(String title)})
: super(key: key);
@override
_UserInputState createState() => _UserInputState();
}
class _UserInputState extends State<UserInput> {
String input = "";
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(), labelText: "ToDo"),
onSubmitted: widget.onSubmit,
onChanged: (value) => input = value,
),
TextButton(
onPressed: () => widget.onSubmit(input),
child: const Text("Save"),
)
],
),
);
}
}

ToDo类具有Map<字符串,bool>包含ToDos并存储它们是否完成。然后,类应该在ListView(带有ListView.builder(中显示这些数据,每个ToDo都通过自定义TodoItem Widget显示。

VSC没有显示任何编译错误,但当我尝试用flutter运行代码时,我会收到以下错误消息:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building ToDo(dirty, dependencies:
[_LocalizationsScope-[GlobalKey#93b03]], state: _ToDoState#a990a):
type 'List<String>' is not a subtype of type 'Map<String, bool>' of 'function result'
The relevant error-causing widget was:
ToDo
ToDo:file:///C:/<path>/tutorial_project_1/lib/main.dart:17:19
#1      _ToDoState.build (package:tutorial_project_1/main.dart:77:20)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4716:27)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:15)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4774:11)
#5      Element.rebuild (package:flutter/src/widgets/framework.dart:4322:5)
#6      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2589:33)
#7      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#8      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#9      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1145:15)    :5)
#10     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#11     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:863:7)                                                                                         g.dart:863:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════════════════════════  
Performing hot reload...
Reloaded 1 of 582 libraries in 285ms.

我以前使用列表而不是地图,但后来我想添加"检查任务"功能,错误开始出现。

我希望你能帮我解决这个问题。

编辑

所以我重新启动了模拟器,发现问题是超过了安卓系统上设置的64k限制,没有启用multidex,与应用程序无关。。。

当我运行您的代码时,我不会收到任何错误。。。我注意到你的复选框onchanged((不起作用,所以我在TodoItem类中修改了它:

leading: Checkbox(
value: done,
onChanged: (value) => onChanged(), // added () after onChanged
),

然后一切都很顺利。

相关内容

最新更新