Search code examples
flutterflutter-dependenciesdart-null-safetyflutter-pluginflutter-packages

How to add a Flutter plugin that does not support Null Safety


Hay, I want to make a "Privacy Policy Checkbox" that includes a Text Hyperlink, and I found Link Plugin in pub.dev that what I want to use, but unfortunately it doesn't support Null Safety. I am running in flutter 2.5.1.

When I used the Link Widget from that package it shows a notification

the library 'package:link/link.dart' is legacy, and should not be imported into a null safe library

and I can't hot reload my project anymore. What other method can use to show a Hyperlink text in flutter?


Solution

  • I am going to make this a longer answer not just specifically for your exact library because this is an issue that I can see happen more often. The options below may not all work in your usecase but will cover other cases as well.

    Option 1 - Check for beta releases

    Sometimes the migration has been completed but not yet released as stable. You can have a look in the Version tab on pub to see if one of the prereleases is marked with the Null Safety badge:

    Screenshot of the prerelease version of the artemis code generator package showing prereleases with the Null Safety badge

    Option 2 - Check the repository

    Sometimes the migration has been completed but not yet released on pub at all. There are a few ways of checking:

    • Look into the pubspec.yaml, the environment.sdk key should list '>=2.12.0 <3.0.0' (the lower bound may be higher; there are libraries that use '>=2.14.0 <3.0.0' but 2.12.0 is required for null safety).
    • Look into pull requests and search for null safety; maybe one is merged already which would indicate that the change is on the branch it was merged on
    • Look into issues and search for null safety; maybe one is closed and the maintainer dropped "Done" or something alike in the thread which would indicate that the change is on the branch it was merged on

    If you find that indeed, the repository is on a version where it's null safe, you can use a git reference in your pubspec file:

      link:
        git:
          url: https://github.com/galonsos/link
          ref: master
    

    Read more about referencing a package from git in the pub docs.

    Option 3 - Check forks of the repository

    This is something you will likely do together with option 2. When scrolling through issues and PRs, you might stumble over someone who says "I did this on my fork" or an open PR by someone who has converted the library over (this happened in your case: relevant PR). If you find an actively maintained fork that meets your requirements, reference that with a git reference in your pubspec file, just as before.

    Option 4 - Open an issue and wait (if none exists yet)

    This obviously depends on whether the author of the package is still active somewhere around or whether you have the time to wait. Either way, it's never a bad idea to open an issue for an issue.

    Option 5 - Do it yourself

    Migrating a library to null safety can be time consuming, depending on its size and complexity. Regardless, it may be your only option in case you have an extremely specific library you don't want to rewrite from the ground up and it's a good exercise to better understand the library and dart itself.

    If you are nice, be the person you hoped you had found before you and open a PR to the original repository when you are done, so someone else is already helped with option 3 :) And, of course, use a git reference to use your newly upgraded package.

    Option 6 - Use an alternative

    This might not always work but could be faster than doing the null safety migration yourself. There are often other packages that do something similar in a slightly different way. And even if not, you can sometimes build a quick alternative yourself. A link can quickly be built using some GestureDetector or InkWell and the url_launcher package (which is the same setup used in the link package) for example.

    Option 7 - Drop sound null safety

    If none of the other options are valid for you, you can drop sound null safety by running your app with --no-sound-null-safety. You can silence the analyzer warning with a comment:

    // ignore: import_of_legacy_library_into_null_safe
    import 'package:link/link.dart';
    

    I am not sure how that impacts hot reloading however and it's a good idea to try and use one of the other options before going for this.