Null Safety Migration: How to migrate an App with intl package?

Issue

I am using intl: ^0.17.0 in the App that I want to migrate to null safety. All my packages, including intl, support null safety so the dart migrate analysis works without a problem and I can go to the migration assistant link.

However when I get there I get errors in both the l10n.dart and each of the translate messages dart files as below.

l10n.dart migration errors

messages translation files errors

As indicated, if I want to migrate to null safety I would have to do some corrections of the type Changed S to be nullable and Changed type MessageLookupByLibrary

However, these files are automatically generated and have a clear message stating that they MUST NOT BE MODIFIED.

// DO NOT EDIT. This is code generated via
package:intl/generate_localized.dart // This is a library that looks
up messages for specific locales by // delegating to the appropriate
library.

// GENERATED CODE – DO NOT MODIFY BY HAND

Is it really possible to migrate an App that uses intl (or is translated into different languages) to null safety?

How can I migrate to null safety if I am using intl package?

Solution

I want to share with you my experience migrating to Null Safety with a relatively large application that uses the intl package to handle different languages. I hope it serves someone else.

Before I have to confess that I was afraid to do this migration because after having all the dependencies updated to null safety and once I ran dart migrate, I got a lot of errors and suggested changes (more than 1000 in 173 files) with which it augured a tedious future.

Among these changes, the one that most concerned me is the one that appears in this question regarding the inconvenience of modifying the files automatically generated by the package.

In this order of ideas I will tell you the process that I carried out to make the migration.

  1. Generate a new git branch apart from the master (I called it migratingToNullSafety). Surely you don’t want to be left in the middle of the migration without the possibility of making an urgent change in the previous version (master)

  2. Run dart migrate and deselect the generated files as suggested by @AmanVerma in the comments

  3. In my case, I accepted almost all the dart migrate suggestions, except for the variables that it was very clear to me that they were not going to arrive in a null state and therefore I had to use the ! Flag. The review was made starting with the files that had less dependencies towards those with more dependencies as suggested in the documentation.

  4. Once the migration is applied, you will likely see a few more errors within the editor. In my case they were relatively easy to correct manually.

  5. As I had not yet migrated the files generated by the intl package, I proceeded in a similar way to the one that appears in intl package Null Safety problems with the following commands:

flutter pub global list // to check the version

flutter pub global activate intl_utils 2.4.1 // to update to the latest version
  1. Once it is executed, a lot of errors appear, but very easy to correct, since the only thing that was required in my case was to change S.of(context)!. by S.of(context). That is, remove the ! flag within the calls of internationalization. To change it in all files I used Cmd + Shift + H in VS Code

  2. When I no longer had errors, I ran the App in the emulator and it opened correctly. I ran tests, mostly where I knew I might have issues with null and made the fixes.

And that’s it. Something that I thought was going to take days of tedious work, it took me a few hours.

I hope it serves someone else !!

Answered By – David L

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *