How can I enable Flutter/Dart language experiments?
Asked Answered
F

9

31

I want to use the new spread syntax in Dart.

I'm using Android Studio for Flutter development and I receive this error.

This requires the --spread-collections experiment to be enabled

ListView(children: [...listTiles, MyWidget()])

However, I didn't find anywhere where I could specify this option.

I couldn't even make it work on the command line. flutter --spread-collections run gives Could not find an option named "spread-collections"..

flutter --version
Flutter 1.3.8 • channel beta • https://github.com/flutter/flutter.git
Framework • revision e5b1ed7a7f (4 weeks ago) • 2019-03-06 14:23:37 -0800
Engine • revision f4951df193
Tools • Dart 2.2.1 (build 2.2.1-dev.0.0 571ea80e11)
Fumikofumitory answered 2/4, 2019 at 14:16 Comment(3)
You need to be on master branchWitten
The experimental features are only on master branch. By default you are on stable branch. You can see channels using flutter channel. Read github.com/flutter/flutter/wiki/Flutter-build-release-channels for more info.Swinford
if you still can't get it to work, this worked for me: #59458933Harquebusier
P
26

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"
Prudery answered 2/4, 2019 at 18:27 Comment(0)
H
24

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"
    
Hecht answered 6/3, 2020 at 5:50 Comment(2)
don't you need to change SDK first and then call "flutter upgrade"Harquebusier
Anyway, you need to remember to restart IDEDagger
H
14

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.

Hour answered 2/6, 2020 at 13:48 Comment(0)
B
4

You are running an old version of flutter

Spreading is available starting at flutter 1.5 and dart 2.3

Run:

flutter upgrade
Bedesman answered 10/5, 2019 at 11:51 Comment(2)
Thanks, please note I asked this questions in April, where Dart 2.3 was not yet available for Flutter in any channels.Fumikofumitory
also depends on which flutter channel you areHarquebusier
D
3

Here's some fix you can try out :

1) Keep the analysis_options.yaml in your root folder with this code:

analyzer:
  enable-experiment:
    - control-flow-collections

2) Don't use brackets {} in between your for loops Ex:

<Widget>[
  for (final category in categories) 
    CategoryWidget(category: category)
],

3) Important step which is probably why it's not working for you: Change your Dart version constraint in the pubspec.yml file in your root folder to 2.5.2 or above.

environment:
  sdk: ">=2.5.2 <3.0.0"
Dysgenic answered 29/4, 2020 at 14:35 Comment(0)
R
3

The current answer that is working in the latest Flutter 1.17.1 or Dart 2.8.2

Create a analysis_options.yaml in the directory of the pubspec.yaml file

Write this code inside the file:

include: package:pedantic/analysis_options.1.8.0.yaml 

linter:
  rules:
    - prefer_spread_collections

For more information head to this documentation

Retrograde answered 14/5, 2020 at 13:32 Comment(2)
I Tried it. But I am getting an error 'include: package:pedantic/analysis_options.1.8.0.yaml ' file cannot be found.Lavona
@ShubhamBansal You need to add pedantic: ^1.8.0 to your dev_dependencies in pubspec.yaml.Nicaragua
M
0

Something completely different yet the same. Our code was built in Flutter v1.12.13hotfix9. when we did a new installation with flutter 1.17.1 the build broke with a similar error. It seems that the new release of Flutter is not backwards compatible on certain parts. We fixed the problem by installing the older version.

Moresque answered 28/5, 2020 at 5:15 Comment(0)
R
0

Run this command from your project's root directory:

dart --enable-experiment=spread-collections lib
Rutherfordium answered 23/11, 2020 at 7:21 Comment(0)
D
0

After updating the environment in pubspec.yml don't forget restart your IDE

environment:
  sdk: ">=2.7.2 <3.0.0"
Dejected answered 8/1, 2021 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.