- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,第一个错误在标题中,我先显示代码:
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 确实令人困惑......请帮我解决这个问题...我真的很感激。
最佳答案
只需在 newTaskTitle 声明之前添加 late
关键字。如下。
late String newTaskTitle;
此错误是因为 dart null 安全性,您无法将 null 类型分配给不可为空的字段。
关于flutter - 错误 : The non-nullable local variable 'newTaskTitle' must be assigned before it can be used,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68014437/
for /f "tokens=*" %%a in ('find /v ":" "%appdata%\gamelauncher\options.txt" ^| find "menu=a"') do (
我在 Javascript 中有一组全局计数器变量: var counter_0 = 0; var counter_1 = 0; var counter_2 = 0; 等等 然后我有一个 Javasc
好的,我正在阅读一些有关 RedBlackTrees 的代码。我注意到这一行“v1 = v2 = v3 = v4;”我理解类似“v1 += v2”(将 v2 添加到 v1 的当前值)和“v1 = v2
我正在为 C# 中的游戏数据加载制作一个 csv 阅读器,我想做的就是从数组(变量)的值声明一个变量,我们可以在 php 中像 $$foo 那样做。喜欢 void csvReader(string s
假设我有变量 内容为“ 123 ”和变量 b123 里面有一些文字。出于某种原因,我想使用变量 作为第二个 var 名称的一部分。像这样的东西: SET a=123 SET b123=some_tex
我对 javascript 有点陌生,我无法通过谷歌搜索找到任何内容,我正在编写一个程序,并且能够执行我所要求的操作: if (Variable == 1 或 Variable == 2 或 Vari
我发现我自己在做这种类型的 IF 语句分配。例如: if($variable == 1 || $variable == "whatever" || $variable == '492') { ...
我的虚拟 PC 在 MS-DOS 6.22 上运行时出现问题。 我需要使用变量 Date ,但我无法得到它,因为每当我尝试回显变量时,它都会显示 %variable%反而。 我在 Windows 控制
尝试运行此代码时: List list = em.createQuery("select balance b from Users where b.userName = '" + user_name.
我有一些代码,其中变量可以是 undefined、null 或正常值。无论变量是 undefined 还是 null,代码都需要做同样的事情。说有没有危险 for (var cur = this.bu
我正在编写一个批处理命令脚本,其中检查环境变量。我需要通过传递所有必需的变量来编写一个 FOR 循环,然后验证它是否已定义,如果未定义,则提示该键的值并永久设置该变量。 问题是我无法取消引用循环变量并
我知道这些是 Rails 的基础知识,但我仍然不知道 = 符号和 => 之间的全部区别以及 @some_variable 之间的区别、@@some_variable 和 :some_variable
我正在使用以下内容创建一个动态变量(PHP 术语中的“变量变量”): foo: "test1" set to-word (rejoin [foo "_result_data"]) array 5 但是
我一直在啃 PHP 套接字服务器和客户端的基础知识 here . 然后我偶然发现了这些行(摘自上面链接的第一个示例,发生在 while 中): if (false === ($buf = socket
这个问题在这里已经有了答案: What does "|=" mean? (pipe equal operator) (6 个答案) 关闭 9 年前。 我正在寻找一些编码来扩展我在 Java 方面的知
如何在 C++ 中从其他变量的值打印变量我只是 C++ 的新手。 在 php 中,我们可以通过其他变量的值来制作/打印一个变量。像这样。 $example = 'foo'; $foo = 'abc';
作为 Ruby on Rails 新手,我明白“@”和“:”引用有不同的含义。我看到了this post在 SO 中,其中描述了一些差异。 @ 表示实例变量(例如@my_selection) :表示别
编程新手/甚至更新。一个小的 go 程序有问题 - 不会编译带有 undefined variable 错误。代码: package main import ( "fmt" "io" "o
我知道其他一些语言,如PHP,支持“变量变量名”的概念--即,字符串的内容可以用作变量名的一部分。。我听说总的来说这不是一个好主意,但我认为它可以解决我在Python代码中遇到的一些问题。。有没有可能
我有两个版本的代码。 版本 1 Launcher.java class Launcher { public static void main(String[] args) {
我是一名优秀的程序员,十分优秀!