Flutter 错误:类型 '(String, ItemPriority) => void' 不是类型 '(String, [ItemPriority]) => dynamic' 的子类型
Posted
技术标签:
【中文标题】Flutter 错误:类型 \'(String, ItemPriority) => void\' 不是类型 \'(String, [ItemPriority]) => dynamic\' 的子类型【英文标题】:Flutter error: type '(String, ItemPriority) => void' is not a subtype of type '(String, [ItemPriority]) => dynamic'Flutter 错误:类型 '(String, ItemPriority) => void' 不是类型 '(String, [ItemPriority]) => dynamic' 的子类型 【发布时间】:2020-10-31 17:33:11 【问题描述】:我是 Flutter 新手,正在开发一个简单的待办事项列表应用程序。对于主页,我想要一个待办事项列表和一个添加新待办事项的按钮。点击“新建”按钮应该会打开一个窗口来输入待办事项的标题,并使用我的自定义单选按钮添加 1-5 的优先级。单击此窗口上的发送/提交按钮应将其添加到待办事项列表中(显示在 ListView 中)。
我收到此错误:类型 '(String, ItemPriority) => void' 不是类型 '(String, [ItemPriority]) => dynamic' 的子类型
它来自第 79 行“TodoInputWidget(this.newTodoItem)”,我试图追踪错误的原因,但我不确定我是否完全理解它。我找不到任何可以生成 ItemPrioritys 列表的地方。
我非常感谢您对此问题的任何见解!
这是我现在拥有的代码:
import 'package:flutter/material.dart';
import 'textInputWidget.dart';
void main()
runApp(MyApp());
enum ItemPriority low, low_med, med, med_high, high
extension ItemPriorityExtension on ItemPriority
RadioModel get radio
switch (this)
case ItemPriority.low:
return new RadioModel(true, '1', Colors.green);
case ItemPriority.low_med:
return new RadioModel(true, '2', Colors.lightGreen);
case ItemPriority.med:
return new RadioModel(true, '3', Colors.yellow);
case ItemPriority.med_high:
return new RadioModel(true, '2', Colors.orange);
case ItemPriority.high:
return new RadioModel(true, '2', Colors.red);
default:
return new RadioModel(true, '3', Colors.yellow);
class TodoItem
String task;
ItemPriority priority;
TodoItem(this.task, this.priority);
class RadioModel
bool isSelected;
String radioNumText;
Color color;
RadioModel(this.isSelected, this.radioNumText, this.color);
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Weekly ToDo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
class MyHomePage extends StatefulWidget
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
List<TodoItem> todos = [];
void newTodoItem(String text, ItemPriority priority)
this.setState(()
todos.add(new TodoItem(text, priority));
);
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(title: Text('Weekly ToDo')),
body: Column(
children: <Widget>[
Expanded(child: TodoList(this.todos)),
TodoInputWidget(this.newTodoItem),
],
),
);
class TodoInputWidget extends StatefulWidget
final Function(String, [ItemPriority]) callback;
TodoInputWidget(this.callback);
@override
_TodoInputWidgetState createState() => _TodoInputWidgetState();
class _TodoInputWidgetState extends State<TodoInputWidget>
String text;
ItemPriority priority;
CustomRadio radio = new CustomRadio(ItemPriority.med);
bool selected = false;
void createTodo(String text)
this.setState(()
widget.callback(text, radio.priority);
);
createTodoWindow(BuildContext context)
return showDialog(
context: context,
builder: (context) => SimpleDialog(
title: Text('Create new task'),
children: <Widget>[radio, TextInputWidget(this.createTodo)],
));
@override
Widget build(BuildContext context)
return GestureDetector(
onTap: ()
createTodoWindow(context);
,
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.green,
),
),
);
class TodoList extends StatefulWidget
final List<TodoItem> todoItems;
TodoList(this.todoItems);
@override
_TodoListState createState() => _TodoListState();
class _TodoListState extends State<TodoList>
@override
Widget build(BuildContext context)
return ListView.builder(
itemCount: this.widget.todoItems.length,
itemBuilder: (context, index)
var todoItem = this.widget.todoItems[index];
return ListTile(title: Text(todoItem.task));
,
);
class RadioItem extends StatelessWidget
final RadioModel item;
RadioItem(this.item);
@override
Widget build(BuildContext context)
return Container(
child: Text(item.radioNumText),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: item.color,
),
alignment: Alignment.center,
);
//ignore: must_be_immutable
class CustomRadio extends StatefulWidget
ItemPriority priority;
CustomRadio(this.priority);
@override
_CustomRadiostate createState() => _CustomRadioState();
class _CustomRadioState extends State<CustomRadio>
List<RadioModel> priorityChoices = new List<RadioModel>();
ItemPriority priority;
@override
void initState()
super.initState();
widget.priority = priority;
priorityChoices.add(ItemPriority.low.radio);
priorityChoices.add(ItemPriority.low_med.radio);
priorityChoices.add(ItemPriority.med.radio);
priorityChoices.add(ItemPriority.med_high.radio);
priorityChoices.add(ItemPriority.high.radio);
@override
Widget build(BuildContext context)
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: priorityChoices.length,
itemBuilder: (context, index)
return IconButton(
onPressed: ()
priorityChoices.forEach((element)
element.isSelected = false;
);
priorityChoices[index].isSelected = true;
priority = ItemPriority.values[index];
,
icon: RadioItem(priorityChoices[index]),
);
,
);
【问题讨论】:
【参考方案1】:替换函数;
dynamic newTodoItem(String text, [ItemPriority priority])
this.setState(()
todos.add(new TodoItem(text, priority));
);
return some.. or null;
【讨论】:
以上是关于Flutter 错误:类型 '(String, ItemPriority) => void' 不是类型 '(String, [ItemPriority]) => dynamic' 的子类型的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 错误:类型 '(String, ItemPriority) => void' 不是类型 '(String, [ItemPriority]) => dynamic' 的子类型
Flutter 'showDatePicker' 抛出异常:错误:预期值为 'String' 类型,但得到类型为 'Null' 的值之一
错误:参数类型“TextEditingController”无法分配给参数类型“String”。在 FLUTTER
Flutter 中的错误:未处理的异常:“Null”类型不是“String”类型的子类型