Fixing "Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized."

I got this error when trying to run an older app in Android Studio, although I had not changed any code nor dependency yet.

A Google search reveals an announcement about the breaking change on the flutter-announce Google group from Aug 9, 2019. See the full post. The fix is then to call WidgetsFlutterBinding.ensureInitialized() in main() before runApp. So my code went from:

void main() => runApp(new MyApp());

To:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(new MyApp());
}