简体   繁体   English

Flutter 错误:68:14:错误:找不到 Getter:“上下文”。 和未处理的异常:“空”类型不是“BuildContext”类型的子类型

[英]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.

相关问题 Flutter [错误:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:“Null”类型不是“BuildContext”类型的子类型 - Flutter [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'BuildContext' 错误:flutter 未处理的异常:类型“Null”不是类型“String”的子类型 - Error:flutter Unhandled Exception: type 'Null' is not a subtype of type 'String' Flutter 中的错误:未处理的异常:“Null”类型不是“String”类型的子类型 - error in Flutter : Unhandled Exception: type 'Null' is not a subtype of type 'String' Flutter - 未处理的异常:未处理的错误类型“int”不是“日期时间”类型的子类型? - Flutter - Unhandled Exception: Unhandled error type 'int' is not a subtype of type 'DateTime?' Flutter json_serializable 模型错误:未处理的异常:类型“Null”不是类型转换中“String”类型的子类型 - Flutter json_serializable models error: Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast Flutter 异常:使用 Navigator 时出现“上下文不是 BuildContext 的子类型”错误 - Flutter exception: 'Context is not a subtype of BuildContext' error using Navigator Flutter 错误:未处理的异常:类型“未来”<dynamic> ' 不是类型 'Future 的子类型<null> ?</null></dynamic> - Flutter Error : Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<Null>? 未处理的异常:类型“Null”不是“String”类型的子类型我收到错误 - Unhandled Exception: type 'Null' is not a subtype of type 'String' I get the error “Context”类型不是“BuildContext”类型的子类型 - type 'Context' is not a subtype of type 'BuildContext' Flutter 未处理的异常:“Null”类型不是“String”类型的子类型 - Flutter Unhandled Exception: type 'Null' is not a subtype of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM