gpt4 book ai didi

flutter - 错误 : The non-nullable local variable 'newTaskTitle' must be assigned before it can be used

转载 作者:行者123 更新时间:2023-12-05 00:53:24 25 4
gpt4 key购买 nike

大家好,第一个错误在标题中,我先显示代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com