Flutter 'showDatePicker' throws exception: Error: Expected a value of type 'String', but got one of type 'Null'

Viewed 872
TextButton(
  onPressed: () => _showPicker(context),
  child: Icon(Icons.calendar_today),
)

Future<DateTime?> _showPicker(BuildContext context) {
  return showDatePicker(
      context: context, initialDate: DateTime(2000),
      firstDate: DateTime(1990),
      lastDate: DateTime(2100));
}

The DatePicker pops up, but when you try to select a date or year it throws: Expected a value of type 'String', but got one of type 'Null'

Flutter 2.0.3 • channel stable • https://github.com/flutter/flutter.git Framework • revision 4d7946a68d (7 days ago) • 2021-03-18 17:24:33 -0700 Engine • revision 3459eb2436 Tools • Dart 2.12.2

3 Answers

I see the same using the latest flutter for web. It looks like the error is within the datepicker itself, not my code? The datepicker still seems to function as expected despite the error message showing in the debug consol?.

EDIT: This has been fixed in the master channel and will release soon

I filed a bug for this: https://github.com/flutter/flutter/issues/79834

This is caused by the framework internally calling HapticFeedback.vibrate() for a fun little buzz when you tap pretty much anything in the pop-up window. However, on Web, this doesn't work. There is a switch-case to try to see if the platform supports it, but Web never got a proper TargetPlatform, so the code thinks computers can vibrate.

When Flutter tries sending a message to the web engine, the null error happens.

I use verison 2.2.3 stable, this issue has been fixed.

Related