0

尽管我的代码没有出错,但无法从我的日期选择器中获取日期。

  Future selectDate() async {
    DateTime picked = await showDatePicker(
        context: context,
        initialDate: new DateTime.now(),
        firstDate: new DateTime(2016),
        lastDate: new DateTime(2222));
    if (picked != null) setState(() => picked = datepicked);
    else{
      print(picked);
    }
  }
}

在此处输入图像描述

4

1 回答 1

1

您需要context在 datepicker 方法中传递。完整的工作代码如下:

body: Center(
        child: RaisedButton(
        child: Text('click'),
        onPressed: () {
          selectDate(context);
        }
        )
      ),


Future selectDate(BuildContext context) async {
    DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: new DateTime(2016),
        lastDate: new DateTime(2222));
    if (picked != null && picked!= selectedDate) {
      setState(() => selectedDate = picked);
      print(picked);
    }
    else{
      print(picked);
    }
  }

// 2020-02-21 00:00:00.000

于 2020-02-14T05:31:00.013 回答