Search code examples
flutterrestdartvariablesdatetimepicker

How to assign the value of selected date to a variable


This is my code

DateTimeFormField(
  decoration: InputDecoration(
    hintStyle: const TextStyle(color: Colors.black),
    errorStyle: const TextStyle(color: Colors.redAccent),
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(20),
    ),
    hintText: 'MM DD, YYYY',
    filled: true,
    fillColor: Colors.grey[200],
    suffixIcon: const Icon(Icons.event_note),
    labelText: 'Select Date',
  ),
  mode: DateTimeFieldPickerMode.date,
  autovalidateMode: AutovalidateMode.always,
  validator: (e) =>
  (e?.day ?? 0) == 1
      ? 'Please not the first day'
      : null,
  onDateSelected: (DateTime value) {
    // value = populdate;
  },
),

I want to put the selected date value into a variable. How to do that, tried many things but didn't get a solution.


Solution

  • i think you already set the value, but forget to update the state

      onDateSelected: (DateTime value) {
        // value that you got on every changes happened
         populdate= value;
        
        print(populdate); // this should be printed an Instance of DateTime
        setState((){}); // update state, and rebuild the UI with latest value
      },