How can I enable Flutter/Dart language experiments?

You are running an old version of flutter

Spreading is available starting at flutter 1.5 and dart 2.3

Run:

flutter upgrade

In my case, I have followed these two steps and It worked for me.

  1. run "flutter upgrade"

  2. changing the sdk in the environment in pubspec.yaml

    environment:
      sdk: ">=2.6.0 <3.0.0"
    

With the new version of flutter it became an error - but it can easily be fixed by updating the sdk version:

environment:
  sdk: ">=2.7.0 <3.0.0" 

Just don't forget to restart VisualStudio Code or whatever IDE you're using.


You need to create an analysis_options.yaml file in the root of your flutter app and write something like

analyzer:
  enable-experiment:
    - spread-collections

Also make sure to switch to the correct channel where the new feature is included eg (dev, beta or master)

flutter channel dev

And also make sure you have a recent enough version of flutter

flutter upgrade

Ensure you are on the right version of flutter and dart that allows that feature by running

flutter --version

you may also have to manually change your pubspec.yaml file to specify the correct dart sdk (if so rerun flutter upgrade)

environment:
  sdk: ">=2.10.0-0 <3.0.0"