无法解决“空值检查运算符用于空值”的问题
我正在尝试在 flutter 中创建一个待办事项列表应用程序。我面临一个小部件的问题,该小部件可以帮助我编辑已创建的待办事项小部件的标题或描述。
这是我的待办事项列表主页的图片
我的编辑按钮的图片
我在按后遇到问题此小部件中的保存按钮
这是我收到的错误
════════ Exception caught by gesture ═══════════════════════════════════════════
The following _CastError was thrown while handling a gesture:
Null check operator used on a null value
When the exception was thrown, this was the stack
#0 _EditTodoPageState.saveTodo
package:todolist/Edit_Todo_Page.dart:47
#1 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:989
#2 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:198
#3 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:608
#4 BaseTapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:296
#5 BaseTapGestureRecognizer.handlePrimaryPointer
package:flutter/…/gestures/tap.dart:230
#6 PrimaryPointerGestureRecognizer.handleEvent
package:flutter/…/gestures/recognizer.dart:563
#7 PointerRouter._dispatch
package:flutter/…/gestures/pointer_router.dart:94
#8 PointerRouter._dispatchEventToRoutes.<anonymous closure>
package:flutter/…/gestures/pointer_router.dart:139
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer: TapGestureRecognizer#25bd2
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(189.1, 309.1)
finalLocalPosition: Offset(67.7, 11.1)
button: 1
sent tap down
════════════════════════════════════════════════════════════════════════════════
这是我的编辑待办事项小部件
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todolist/todo.dart';
import './todos.dart';
import './todo_form_widget.dart';
class EditTodoPage extends StatefulWidget {
final Todo todo;
const EditTodoPage({Key? key, required this.todo}) : super(key: key);
@override
State<EditTodoPage> createState() => _EditTodoPageState();
}
class _EditTodoPageState extends State<EditTodoPage> {
final _formKey = GlobalKey<FormState>();
late String title;
late String description;
@override
void initState() {
super.initState();
title = widget.todo.title;
description = widget.todo.description;
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Edit Todo"),
),
body: Padding(
padding: EdgeInsets.all(16),
child: Form(
child: TodoFormWidget(
title: title,
description: description,
onChangedTitle: (title) => setState(() => this.title = title),
onChangedDescription: (description) =>
setState(() => this.description = description),
onSavedTodo: saveTodo,
),
),
));
}
saveTodo() {
final isValid = _formKey.currentState!.validate();
if (!isValid) {
return;
} else {
final provider = Provider.of<TodosProvider>(context, listen: false);
provider.updateTodo(widget.todo, title, description);
Navigator.of(context).pop();
}
}
}
我在“saveTodo()”方法中收到错误。我无法解决这个错误。
这是我的 Provider 小部件
import 'package:flutter/cupertino.dart';
import 'package:todolist/todo.dart';
import 'package:provider/provider.dart';
class TodosProvider extends ChangeNotifier {
List<Todo> _todos = [
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning
I am trying to create a to-do list app in flutter. I am facing an issue with a widget that helps me to edit an already created to-do widget with respect to the title or description.
This is the pic of my todo list HomePage
This pic of my edit button
I am facing an issue after I press the save button in this widget
This is the error that I am getting
════════ Exception caught by gesture ═══════════════════════════════════════════
The following _CastError was thrown while handling a gesture:
Null check operator used on a null value
When the exception was thrown, this was the stack
#0 _EditTodoPageState.saveTodo
package:todolist/Edit_Todo_Page.dart:47
#1 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:989
#2 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:198
#3 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:608
#4 BaseTapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:296
#5 BaseTapGestureRecognizer.handlePrimaryPointer
package:flutter/…/gestures/tap.dart:230
#6 PrimaryPointerGestureRecognizer.handleEvent
package:flutter/…/gestures/recognizer.dart:563
#7 PointerRouter._dispatch
package:flutter/…/gestures/pointer_router.dart:94
#8 PointerRouter._dispatchEventToRoutes.<anonymous closure>
package:flutter/…/gestures/pointer_router.dart:139
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer: TapGestureRecognizer#25bd2
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(189.1, 309.1)
finalLocalPosition: Offset(67.7, 11.1)
button: 1
sent tap down
════════════════════════════════════════════════════════════════════════════════
This is my edit todo widget
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todolist/todo.dart';
import './todos.dart';
import './todo_form_widget.dart';
class EditTodoPage extends StatefulWidget {
final Todo todo;
const EditTodoPage({Key? key, required this.todo}) : super(key: key);
@override
State<EditTodoPage> createState() => _EditTodoPageState();
}
class _EditTodoPageState extends State<EditTodoPage> {
final _formKey = GlobalKey<FormState>();
late String title;
late String description;
@override
void initState() {
super.initState();
title = widget.todo.title;
description = widget.todo.description;
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Edit Todo"),
),
body: Padding(
padding: EdgeInsets.all(16),
child: Form(
child: TodoFormWidget(
title: title,
description: description,
onChangedTitle: (title) => setState(() => this.title = title),
onChangedDescription: (description) =>
setState(() => this.description = description),
onSavedTodo: saveTodo,
),
),
));
}
saveTodo() {
final isValid = _formKey.currentState!.validate();
if (!isValid) {
return;
} else {
final provider = Provider.of<TodosProvider>(context, listen: false);
provider.updateTodo(widget.todo, title, description);
Navigator.of(context).pop();
}
}
}
I am getting an error in the "saveTodo()" method. I am not able to solve this error.
This is my Provider widget
import 'package:flutter/cupertino.dart';
import 'package:todolist/todo.dart';
import 'package:provider/provider.dart';
class TodosProvider extends ChangeNotifier {
List<Todo> _todos = [
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning????!!!",
),
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning????!!!",
),
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning????!!!",
),
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning????!!!",
),
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning????!!!",
),
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning????!!!",
),
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning????!!!",
),
Todo(
createdTime: DateTime.now(),
title: "Wake up at 6:00 AM in the morning????!!!",
),
];
List<Todo> get todos => _todos.where((todo) => todo.isDone == false).toList();
List<Todo> get todosCompleted =>
_todos.where((todo) => todo.isDone == true).toList();
void addTodo([Todo? todo]) {
_todos.add(todo!);
notifyListeners();
}
void removeTodo(Todo todo) {
_todos.remove(todo);
notifyListeners();
}
bool toggleTodoStatus(Todo todo) {
todo.isDone = !todo.isDone;
notifyListeners();
return todo.isDone;
}
void updateTodo(Todo todo, String title, String description) {
todo.title = title;
todo.description = description;
notifyListeners();
}
}
Please help me solve this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,您没有在
Form
小部件中使用_formKey
,这就是_formKey
currentState
变为 null 的原因。尝试在Form
小部件中使用_formKey
并检查它不等于 null。I think, you haven't used
_formKey
inForm
widget thats why_formKey
currentState
is getting null. Try to use_formKey
in theForm
widget and check it is not equal to null.