title: How can I retrieve date that user picked in DatePicker in Flutter?
body: <p>I am making an application that includes DateTime Line Chart and DatePicker. I want a user to pick the time range in DatePicker. I want to display data from a picked time period on the chart.
  I don't know how can I retrieve the start and the end of time range and use it on my chart.</p>
  <p>Here are parts of my code:</p>
  <pre class="lang-dart prettyprint-override"><code>Widget build(BuildContext context) {
    return MaterialApp(
      Expanded(
        child: ButtonWidget(
          text: getFrom(),
          onClicked: () =&gt; pickDateRange(context),
        ),
      ),
      const SizedBox(width: 8),
      Icon(Icons.arrow_forward, color: Colors.white),
      const SizedBox(width: 8),
      Expanded(
        child: ButtonWidget(
          text: getUntil(),
          onClicked: () =&gt; pickDateRange(context),
        ),
      ),
      Expanded(
          child: SfCartesianChart(
              primaryXAxis: DateTimeAxis(
                  minimum: DateTime(2021, 12, 02, 00, 00, 00),
                  maximum: DateTime(2021, 12, 02, 01, 18, 12)),
              primaryYAxis: NumericAxis(minimum: 30, maximum: 130),
              series: &lt;ChartSeries&lt;LineChart, DateTime&gt;&gt;[
            // Renders line chart
            LineSeries&lt;LineChart, DateTime&gt;(
                dataSource: lineDataHR,
                xValueMapper: (LineChart sales, _) =&gt; sales.date,
                yValueMapper: (LineChart sales, _) =&gt; sales.hr)
          ])),
    );
  }
  
  Future pickDateRange(BuildContext context) async {
    final initialDateRange = DateTimeRange(
      start: DateTime.now(),
      end: DateTime.now().add(Duration(hours: 24 * 2)),
    );
    final newDateRange = await showDateRangePicker(
      context: context,
      firstDate: DateTime(DateTime.now().year - 1),
      lastDate: DateTime(DateTime.now().year + 1),
      initialDateRange: dateRange ?? initialDateRange,
    );
  
    if (newDateRange == null) return;
  
    setState(() =&gt; dateRange = newDateRange);
  }
  
  String getFrom() {
    if (dateRange == null) {
      return 'Od';
    } else {
      return DateFormat('dd/MM/yyyy').format(dateRange.start);
    }
  }
  
  String getUntil() {
    if (dateRange == null) {
      return 'Do';
    } else {
      return DateFormat('dd/MM/yyyy').format(dateRange.end);
    }
  }
  </code></pre>