【发布时间】:2021-06-17 07:02:31
【问题描述】:
大家好,第一个错误在标题中,让我先显示代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todoye_app_angela/models/task_data.dart';
class AddTaskScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
String newTaskTitle;
return Container(
color: Color(0xFF757575),
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.limeAccent[400],
borderRadius: BorderRadius.only(
topLeft: Radius.circular(60),
topRight: Radius.circular(60),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 30, color: Colors.brown[900]),
),
SizedBox(
height: 12,
),
TextField(
onChanged: (newText) {
newTaskTitle = newText;
},
autocorrect: false,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.brown[800]!,
width: 2,
),
borderRadius: BorderRadius.circular(30),
),
hintText: 'Type Your Task ...',
labelStyle: TextStyle(
color: Colors.green[900],
),
helperStyle: TextStyle(
color: Colors.brown[900],
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(60),
borderSide: BorderSide(
color: Colors.brown[900]!,
width: 2,
),
),
),
),
SizedBox(
height: 12,
),
ElevatedButton(
onPressed: () {
Provider.of<TaskData>(context, listen: false)
.addTask(newTaskTitle);
Navigator.pop(context);
},
child: Text(
'Add',
style: TextStyle(color: Colors.brown[900]),
),
style: ButtonStyle(
backgroundColor: MaterialStateColor.resolveWith(
(states) => Colors.lightGreen),
elevation: MaterialStateProperty.resolveWith((states) => 6),
shadowColor:
MaterialStateColor.resolveWith((states) => Colors.green),
minimumSize: MaterialStateProperty.resolveWith(
(states) => Size.square(40.67)),
),
),
],
),
),
);
}
}
错误出现在我试图将此值添加到“addTask”方法的 onPress 提升按钮中...我已通过以下更改修复了此错误:
String? newTaskTitle;
现在我收到了这个新错误: 错误:参数类型“字符串?”不能分配给参数类型“字符串”。 它指向这个方法:
void addTask(String newTaskTitle) {
_tasks.add(Task(name: newTaskTitle));
notifyListeners();
}
我也通过将其更改为这个来修复它:
void addTask(String? newTaskTitle) {
_tasks.add(Task(name: newTaskTitle));
notifyListeners();
}
现在我收到了这个新错误: 错误:参数类型“字符串?”不能分配给参数类型“字符串”。 指向此代码:
class Task {
final String name;
bool isDone;
Task({
required this.name,
this.isDone = false,
});
void toggleDone() {
isDone = !isDone;
}
}
我也修复了它,这是更改:
class Task {
final String? name;
bool isDone;
Task({
required this.name,
this.isDone = false,
});
void toggleDone() {
isDone = !isDone;
}
}
现在又出现了这个错误: 错误:参数类型“字符串?”不能分配给参数类型“字符串”。似乎这个错误爱我!?... 指向此代码{这是整个文件}:
class TaskList extends StatelessWidget {
@override
Widget build(BuildContext context) {
// the consumer widget comes from the provider package we simply wrap it around the widget we want to listen to ....we have to specify the data type we want to have access to then...
return Consumer<TaskData>(
// then we provide the builder property which takes 3 arguments => 1: context : where we are in the widget tree ? second: it will provide the current data [Provider.of<TaskData>(context).task] and we can give that object a name ..here we named it taskData and at last it takes a property called child.
builder: (context, taskData, child) {
// here we return any widget that is needed to built based on this taskData .
return ListView.builder(
itemCount: taskData.taskCount,
itemBuilder: (context, index) {
return TaskTile(
taskTitle: taskData.tasks[index].name,
isChecked: taskData.tasks[index].isDone,
checkboxCallback: (checkboxState) {
// setState(() {
// Provider.of<TaskData>(context).tasks[index].toggleDone();
// });
},
);
},
);
},
);
}
}
它指向这条线:
taskTitle: taskData.tasks[index].name,
这让我找到了这个文件:
class TaskTile extends StatelessWidget {
final String taskTitle;
final bool isChecked;
final ValueChanged<bool?> checkboxCallback;
const TaskTile({
required this.taskTitle,
required this.isChecked,
required this.checkboxCallback,
});
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(
taskTitle,
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),
),
trailing: Checkbox(
activeColor: Colors.lime,
value: isChecked,
onChanged: checkboxCallback,
// onChanged: toggleCheckBoxState,
),
);
}
}
此文件要我更改此代码:
final String taskTitle;
到这个:
final String? taskTitle;
最后是这段代码:
title: Text(
taskTitle,
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),
),
到这个:
title: Text(
taskTitle!,
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),
),
然后我重新启动了应用程序并尝试将新任务添加到列表中,但我收到此错误: 用于空值的空检查运算符 它指向这段代码的这一部分:
title: Text(
taskTitle!,
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),
),
dart 中的 Null Safety 确实令人困惑 .. 它造成了一个混乱循环,不会让程序员安心地编写代码 .... 请帮我解决这个问题... 我真的很感激。
【问题讨论】:
-
“它造成了一个混乱的循环,使程序员无法平静地编写代码”......您可以通过检查
null来打破外观。例如:onPressed: () { if (newTaskTitle != null) { Provider.of<TaskData>(context, listen: false).addTask(newTaskTitle); } }. -
我之前尝试过,但它不起作用。问题出在错误中:错误:必须先分配不可为空的局部变量“newTaskTitle”,然后才能使用它。如何解决这个问题?
-
不,因为你的变量是有条件地初始化的,你需要使它可以为空并添加空检查。检查不可为空的变量是否为空是没有意义的。或者使其不可为空,但使用默认的非空值对其进行初始化。