[英]Flutter Error :68:14: Error: Getter not found: 'context'. and Unhandled Exception: type 'Null' is not a subtype of type 'BuildContext'
i didn't know what is the error means by that.but the thing is i want passing in my funtion of ShowDate Widget inside my Button function here is the code:我不知道那个错误是什么意思。但问题是我想在我的 Button 函数中传入我的 ShowDate Widget 功能,这里是代码:
class AddTaskPage extends StatefulWidget {
const AddTaskPage({Key? key}) : super(key: key);
@override
_AddTaskPageState createState() => _AddTaskPageState();
}
class _AddTaskPageState extends State<AddTaskPage> {
DateTime _selectedDate = DateTime.now();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: context.theme.backgroundColor,
toolbarHeight: 80,
leading: GestureDetector(
child: Icon(Icons.arrow_back_ios),
onTap: () {
return Get.back();
},
),
),
body: Container(
padding: EdgeInsets.only(left: 20, right: 20),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Add Task",
style: TextStyle(
fontSize: 30,
fontFamily: "Lato",
fontWeight: FontWeight.bold),
),
MyInputField(title: "Title", hint: "Enter Your Title"),
MyInputField(title: "Note", hint: "Enter Your Note"),
MyInputField(
title: "Date",
hint: DateFormat.yMd().format(_selectedDate),
widget: IconButton(
icon: Icon(
Icons.date_range,
color: Colors.red,
),
onPressed: () {
_getDateFromUser();
},
),
),
],
),
),
),
backgroundColor: context.theme.backgroundColor,
);
}
}
_getDateFromUser() async {
DateTime? _pickerDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2015),
lastDate: DateTime(2121),
);
}
And at the bottom it's where the problem is occured with the context properties:底部是上下文属性出现问题的地方:
_getDateFromUser() async {
DateTime? _pickerDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2015),
lastDate: DateTime(2121),
);
}
when i hover over it with VScode the quickfixes is saying that i need to provide the local variable,it work afterward but when i click the icon button it says the following error in the terminal:当我用 VScode 将鼠标悬停在它上面时,quickfixes 说我需要提供局部变量,然后它可以工作,但是当我单击图标按钮时,它在终端中显示以下错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'BuildContext'
how to handle that error ?如何处理该错误? what is that mean ?那什么意识 ?
If you declare outside, you must need to pass context.如果在外部声明,则必须传递上下文。
_getDateFromUser(BuildContext context) async {
DateTime? _pickerDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2015),
lastDate: DateTime(2121),
);
}
``
void pickingdate() {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2021),
lastDate: DateTime.now())
.then((pickedDate) {
if (pickedDate == null) {
return;
}
setState(() {
_pickerDate = pickedDate;
});
});
print('...');
}
and call function without parenthesis like并调用不带括号的函数,例如
onPressed: pickingdate,
pickingdate
inside onPressed
is a function reference, which basically means it is not executed immediately, it is executed after the user clicks on the specific widget.(callback) pickingdate
里面的onPressed
是一个函数引用,基本上就是说它不是立即执行,而是在用户点击特定的widget之后执行。(回调)
pickingdate()
is a function call and it is executed immediately. pickingdate()
是一个函数调用,它会立即执行。
Therefore, inside onPressed
you can either pass a function reference or an anonymous function that will act as a callback.因此,在onPressed
您可以传递函数引用或充当回调的匿名函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.