所需的命名参数,但没有相应的参数。我应该怎么办?
我一直在我的Flutter应用程序中遇到以下相关错误:
需要命名参数“描述”,但没有 相应的参数。尝试添加所需的参数。
需要命名的参数“ ID”,但没有相应的 争论。尝试添加所需的参数。
我在以下情况下收到此错误:
Task _newTask = Task(title: value);
这是我的 taskpage.dart
// ignore_for_file: prefer_const_constructors
import 'package:flutter/material.dart';
import 'package:flutter_to_do/database_helper.dart';
import 'package:flutter_to_do/models/task.dart';
import 'package:flutter_to_do/models/todo.dart';
import 'package:flutter_to_do/widgets.dart';
class Taskpage extends StatefulWidget {
final Task task;
Taskpage({required this.task});
@override
_TaskpageState createState() => _TaskpageState();
}
class _TaskpageState extends State<Taskpage> {
DatabaseHelper _dbHelper = DatabaseHelper();
int _taskId = 0;
String _taskTitle = "";
String _taskDescription = "";
late FocusNode _titleFocus;
late FocusNode _descriptionFocus;
late FocusNode _todoFocus;
bool _contentVisile = false;
@override
void initState() {
if (widget.task != null) {
// Set visibility to true
_contentVisile = true;
_taskTitle = widget.task.title;
_taskDescription = widget.task.description;
_taskId = widget.task.id;
}
_titleFocus = FocusNode();
_descriptionFocus = FocusNode();
_todoFocus = FocusNode();
super.initState();
}
@override
void dispose() {
_titleFocus.dispose();
_descriptionFocus.dispose();
_todoFocus.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(
top: 24.0,
bottom: 6.0,
),
child: Row(
children: [
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Image(
image: AssetImage(
'assets/images/back_arrow_icon.png'),
),
),
),
Expanded(
child: TextField(
focusNode: _titleFocus,
onSubmitted: (value) async {
// Check if the field is not empty
if (value != "") {
// Check if the task is null
if (widget.task == null) {
Task _newTask = Task(title: value);
_taskId =
await _dbHelper.insertTask(_newTask);
setState(() {
_contentVisile = true;
_taskTitle = value;
});
} else {
await _dbHelper.updateTaskTitle(
_taskId, value);
print("Task Updated");
}
_descriptionFocus.requestFocus();
}
},
controller: TextEditingController()
..text = _taskTitle,
// ignore: prefer_const_constructors
decoration: InputDecoration(
hintText: "Enter Task Title",
border: InputBorder.none,
),
style: TextStyle(
fontSize: 26.0,
fontWeight: FontWeight.bold,
color: Color(0xFF211551),
),
),
)
],
),
),
Visibility(
visible: _contentVisile,
child: Padding(
padding: EdgeInsets.only(
bottom: 12.0,
),
child: TextField(
focusNode: _descriptionFocus,
onSubmitted: (value) async {
if (value != "") {
if (_taskId != 0) {
await _dbHelper.updateTaskDescription(
_taskId, value);
_taskDescription = value;
}
}
_todoFocus.requestFocus();
},
controller: TextEditingController()
..text = _taskDescription,
decoration: InputDecoration(
hintText: "Enter Description for the task...",
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(
horizontal: 24.0,
),
),
),
),
),
Visibility(
visible: _contentVisile,
child: FutureBuilder(
initialData: [],
future: _dbHelper.getTodo(_taskId),
builder: (context, AsyncSnapshot snapshot) {
return Expanded(
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () async {
if (snapshot.data[index].isDone == 0) {
await _dbHelper.updateTodoDone(
snapshot.data[index].id, 1);
} else {
await _dbHelper.updateTodoDone(
snapshot.data[index].id, 0);
}
setState(() {});
},
child: TodoWidget(
text: snapshot.data[index].title,
isDone: snapshot.data[index].isDone == 0
? false
: true,
),
);
},
),
);
},
),
),
Visibility(
visible: _contentVisile,
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 24.0,
),
child: Row(
children: [
Container(
width: 20.0,
height: 20.0,
margin: EdgeInsets.only(
right: 12.0,
),
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(6.0),
border: Border.all(
color: Color(0xFF86829D), width: 1.5)),
child: Image(
image: AssetImage('assets/images/check_icon.png'),
),
),
Expanded(
child: TextField(
focusNode: _todoFocus,
controller: TextEditingController()..text = "",
onSubmitted: (value) async {
// Check if the field is not empty
if (value != "") {
if (_taskId != 0) {
DatabaseHelper _dbHelper = DatabaseHelper();
Todo _newTodo = Todo(
title: value,
isDone: 0,
taskId: _taskId,
);
await _dbHelper.insertTodo(_newTodo);
setState(() {});
_todoFocus.requestFocus();
} else {
print("Task doesn't exist");
}
}
},
decoration: InputDecoration(
hintText: "Enter Todo item...",
border: InputBorder.none,
),
),
),
],
),
),
)
],
),
Visibility(
visible: _contentVisile,
child: Positioned(
bottom: 24.0,
right: 24.0,
child: GestureDetector(
onTap: () async {
if (_taskId != 0) {
await _dbHelper.deleteTask(_taskId);
Navigator.pop(context);
}
},
child: Container(
width: 60.0,
height: 60.0,
decoration: BoxDecoration(
color: Color(0xFFFE3577),
borderRadius: BorderRadius.circular(20.0),
),
child: Image(
image: AssetImage(
"assets/images/delete_icon.png",
),
),
),
),
),
)
],
),
),
),
);
}
}
这是我的 task.dart
class Task {
final int id;
final String title;
final String description;
Task({required this.id, required this.title, required this.description});
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'description': description,
};
}
}
我该怎么办来解决这个问题?我试图使用
最终字符串?描述
并删除必需命令,但给出了不同的错误。
I've been getting the following related errors at my Flutter application:
The named parameter 'description' is required, but there's no
corresponding argument. Try adding the required argument.The named parameter 'id' is required, but there's no corresponding
argument. Try adding the required argument.
I am receiving this errors at:
Task _newTask = Task(title: value);
Here is my taskpage.dart
// ignore_for_file: prefer_const_constructors
import 'package:flutter/material.dart';
import 'package:flutter_to_do/database_helper.dart';
import 'package:flutter_to_do/models/task.dart';
import 'package:flutter_to_do/models/todo.dart';
import 'package:flutter_to_do/widgets.dart';
class Taskpage extends StatefulWidget {
final Task task;
Taskpage({required this.task});
@override
_TaskpageState createState() => _TaskpageState();
}
class _TaskpageState extends State<Taskpage> {
DatabaseHelper _dbHelper = DatabaseHelper();
int _taskId = 0;
String _taskTitle = "";
String _taskDescription = "";
late FocusNode _titleFocus;
late FocusNode _descriptionFocus;
late FocusNode _todoFocus;
bool _contentVisile = false;
@override
void initState() {
if (widget.task != null) {
// Set visibility to true
_contentVisile = true;
_taskTitle = widget.task.title;
_taskDescription = widget.task.description;
_taskId = widget.task.id;
}
_titleFocus = FocusNode();
_descriptionFocus = FocusNode();
_todoFocus = FocusNode();
super.initState();
}
@override
void dispose() {
_titleFocus.dispose();
_descriptionFocus.dispose();
_todoFocus.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(
top: 24.0,
bottom: 6.0,
),
child: Row(
children: [
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Image(
image: AssetImage(
'assets/images/back_arrow_icon.png'),
),
),
),
Expanded(
child: TextField(
focusNode: _titleFocus,
onSubmitted: (value) async {
// Check if the field is not empty
if (value != "") {
// Check if the task is null
if (widget.task == null) {
Task _newTask = Task(title: value);
_taskId =
await _dbHelper.insertTask(_newTask);
setState(() {
_contentVisile = true;
_taskTitle = value;
});
} else {
await _dbHelper.updateTaskTitle(
_taskId, value);
print("Task Updated");
}
_descriptionFocus.requestFocus();
}
},
controller: TextEditingController()
..text = _taskTitle,
// ignore: prefer_const_constructors
decoration: InputDecoration(
hintText: "Enter Task Title",
border: InputBorder.none,
),
style: TextStyle(
fontSize: 26.0,
fontWeight: FontWeight.bold,
color: Color(0xFF211551),
),
),
)
],
),
),
Visibility(
visible: _contentVisile,
child: Padding(
padding: EdgeInsets.only(
bottom: 12.0,
),
child: TextField(
focusNode: _descriptionFocus,
onSubmitted: (value) async {
if (value != "") {
if (_taskId != 0) {
await _dbHelper.updateTaskDescription(
_taskId, value);
_taskDescription = value;
}
}
_todoFocus.requestFocus();
},
controller: TextEditingController()
..text = _taskDescription,
decoration: InputDecoration(
hintText: "Enter Description for the task...",
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(
horizontal: 24.0,
),
),
),
),
),
Visibility(
visible: _contentVisile,
child: FutureBuilder(
initialData: [],
future: _dbHelper.getTodo(_taskId),
builder: (context, AsyncSnapshot snapshot) {
return Expanded(
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () async {
if (snapshot.data[index].isDone == 0) {
await _dbHelper.updateTodoDone(
snapshot.data[index].id, 1);
} else {
await _dbHelper.updateTodoDone(
snapshot.data[index].id, 0);
}
setState(() {});
},
child: TodoWidget(
text: snapshot.data[index].title,
isDone: snapshot.data[index].isDone == 0
? false
: true,
),
);
},
),
);
},
),
),
Visibility(
visible: _contentVisile,
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 24.0,
),
child: Row(
children: [
Container(
width: 20.0,
height: 20.0,
margin: EdgeInsets.only(
right: 12.0,
),
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(6.0),
border: Border.all(
color: Color(0xFF86829D), width: 1.5)),
child: Image(
image: AssetImage('assets/images/check_icon.png'),
),
),
Expanded(
child: TextField(
focusNode: _todoFocus,
controller: TextEditingController()..text = "",
onSubmitted: (value) async {
// Check if the field is not empty
if (value != "") {
if (_taskId != 0) {
DatabaseHelper _dbHelper = DatabaseHelper();
Todo _newTodo = Todo(
title: value,
isDone: 0,
taskId: _taskId,
);
await _dbHelper.insertTodo(_newTodo);
setState(() {});
_todoFocus.requestFocus();
} else {
print("Task doesn't exist");
}
}
},
decoration: InputDecoration(
hintText: "Enter Todo item...",
border: InputBorder.none,
),
),
),
],
),
),
)
],
),
Visibility(
visible: _contentVisile,
child: Positioned(
bottom: 24.0,
right: 24.0,
child: GestureDetector(
onTap: () async {
if (_taskId != 0) {
await _dbHelper.deleteTask(_taskId);
Navigator.pop(context);
}
},
child: Container(
width: 60.0,
height: 60.0,
decoration: BoxDecoration(
color: Color(0xFFFE3577),
borderRadius: BorderRadius.circular(20.0),
),
child: Image(
image: AssetImage(
"assets/images/delete_icon.png",
),
),
),
),
),
)
],
),
),
),
);
}
}
here it is my task.dart
class Task {
final int id;
final String title;
final String description;
Task({required this.id, required this.title, required this.description});
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'description': description,
};
}
}
What can I do to solve this issue? I tried to use
final String? description
and remove the required command, but it gave different errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要么可以使描述可根据您已经尝试过,因此不需要:
或指定默认值,而不是
必需的
:可能丢弃的任何其他错误可能与您期望描述无效的任何其他错误。
Either you can make description nullable and not required as you already tried:
or specify a default value instead of the
required
:Any other errors that may be thrown are probably related to you not expecting description to be nullable.