使用get.dart方法时,_CastError(对Null值使用的Null检查运算符)


import 'package:calendar_app/consts/routes.dart';
import 'package:calendar_app/controller/task_controller.dart';
import 'package:calendar_app/models/task.dart';
import 'package:calendar_app/services/notification_service.dart';
import 'package:calendar_app/widgets/button.dart';
import 'package:calendar_app/widgets/task_tile.dart';
import 'package:date_picker_timeline/date_picker_timeline.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:intl/intl.dart';
import 'dart:developer' as devtools show log;
import 'package:get/get.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
DateTime _selectDate = DateTime.now();
final _taskController = Get.put(TaskController());
var notif;
@override
void initState() {
super.initState();
notif = Notif();
notif.initializeNotification();
notif.requestIOSPermissions();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color.fromARGB(255, 202, 202, 202),
body: Column(
children: [
Container(
margin: const EdgeInsets.fromLTRB(20, 50, 20, 0),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
// Date
DateFormat.yMMMMd().format(DateTime.now()),
style: const TextStyle(
fontSize: 20,
),
),
const SizedBox(
height: 10,
),
const Text(
// Today
'Today',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
],
),
MyButton(
// Add Task
label: '+ Add Task',
onPressed: () async {
await Navigator.of(context).pushNamed(
addTaskRoute,
// (route) => false,
);
},
),
],
),
const SizedBox(
height: 20,
),
DatePicker(
// Calendar
DateTime.now(),
height: 100,
width: 80,
initialSelectedDate: DateTime.now(),
selectionColor: const Color.fromARGB(255, 12, 103, 179),
selectedTextColor: Colors.white,
dateTextStyle: const TextStyle(
fontSize: 35,
color: Colors.grey,
),
dayTextStyle: const TextStyle(
fontSize: 15,
color: Colors.grey,
),
monthTextStyle: const TextStyle(
fontSize: 15,
color: Colors.grey,
),
onDateChange: (date) {},
),
const SizedBox(
height: 20,
),
],
),
),
_showTasks(),
],
),
);
}
_showTasks() {
return Expanded(
child: Obx(
() {
return ListView.builder(
itemCount: _taskController.taskList.length,
itemBuilder: (_, index) {
devtools.log(_taskController.taskList.length.toString());
return AnimationConfiguration.staggeredList(
position: index,
child: SlideAnimation(
child: FadeInAnimation(
child: Row(
children: [
GestureDetector(
onTap: () {
devtools.log('tapped');
_showBottomSheet(
context,
_taskController.taskList[index],
);
},
child: TaskTile(
_taskController.taskList[index],
),
)
],
),
),
),
);
},
);
},
),
);
}
_showBottomSheet(BuildContext context, Task task) {
Get.bottomSheet(
Container(
padding: const EdgeInsets.only(top: 3),
height: task.isCompleted == 1
? MediaQuery.of(context).size.height * 0.25
: MediaQuery.of(context).size.height * 0.35,
color: Colors.white,
),
);
}
_bottomSheetButton({
required String label,
required Function()? onTap,
required Color color,
bool isClose = false,
required BuildContext context,
}) {
return GestureDetector(
onTap: onTap,
child: Container(
margin: const EdgeInsets.symmetric(vertical: 4),
height: 55,
width: MediaQuery.of(context).size.width * 0.9,
decoration: BoxDecoration(
border: Border.all(
width: 2,
color: isClose == true ? Colors.red : color,
),
borderRadius: BorderRadius.circular(20),
color: isClose == true ? Colors.red : color,
),
),
);
}
}

当我点击容器时,它抛出一个异常,说:

_CastError (Null check operator used on a null value)

我在这里没有使用任何null运算符
这是Get.bottomsheet方法的问题吗<br?异常显示_showBottomSheet有问题,我指的是null运算符或其他什么
你能帮我处理一下吗?

在方法_showBottomSheet中,Task是必需变量。

_taskController.taskList[index]->这可能是null,您应该调试它。

在您的方法中,您可以将Task设置为可选,如下所示:Task?任务,并这样调用您的方法:

_显示底部工作表(上下文_taskController.taskList?[索引],);

然后:

_showBottomSheet(BuildContext context, Task? task) {
Get.bottomSheet(
Container(
padding: const EdgeInsets.only(top: 3),
height: task?.isCompleted == 1
? MediaQuery.of(context).size.height * 0.25
: MediaQuery.of(context).size.height * 0.35,
color: Colors.white,
),
);
}