Update your Flutter project to Flutter 2.0

Upgrade and Migrate Flutter project to 2.0

Follow these steps:

I want to update my current project to Flutter 2.0 to make use of the new dart sound null-safety
— Problem InShort
  1. Run flutter upgrade in the terminal to upgrade Flutter
  2. Run dart migrate to run the dart migration tool
  3. Solve all errors which the migration tool shows
  4. Run flutter pub outdated --mode=null-safety to print all outdated packages
Screenshot 2021-03-09 at 21.18.05.png

You can see if the packages you depend upon support null-safety.

  1. Run flutter pub upgrade --null-safety to upgrade all packages automatically
  2. Check the code for errors and solve them
  3. Run dart migrate again and it should now be successfull. Follow the link to checkout the proposed changes
  4. Press the "Apply Migration" button
  5. Check the code for errors again and fix them

Congratulations, when finished you should now be able to run the app with sound null-safety.

Run flutter run in the command line and the application should run with the command line displaying:

Screenshot 2021-03-09 at 22.14.28.png

Custom ThemeData Flutter - Extension

In Flutter it is possible to adapt the colors of the app to the current theme of the app - light or dark theme. However, the ThemeData object only has a set of predefined colors which can be changed e.g. scaffoldBackgroundColor, color of the AppBarTheme etc. If you wanted a specific color for a icon in your app which changes also according to the theme, it is not possible to specify on the ThemeData.

With this code it is possible to do just that with a CustomThemeData:

I need a custom Color which changes, when the AppTheme changes.
— Problem InShort

The CustomAppTheme is an InheritedWidget and therefore the CustomAppThemeData can be retrieved the same way ThemeData is.

CustomAppThemeData can be appended with your custom colors.

It is important to note that the CustomAppThemeData is only available in child widgets of the CustomAppTheme widget, so it is desired to have the CustomAppTheme as high up in the widget tree as possible.

The implemenation is very similar to the internal implementation of the ThemeData usage, it is very fast.