Home > Net >  Flutter Error :68:14: Error: Getter not found: 'context'. and Unhandled Exception: type &#
Flutter Error :68:14: Error: Getter not found: 'context'. and Unhandled Exception: type &#

Time:10-27

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:

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:

[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 ?

CodePudding user response:

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),
  );
}
``

CodePudding user response:

  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() is a function call and it is executed immediately.

Therefore, inside onPressed you can either pass a function reference or an anonymous function that will act as a callback.

  • Related