How can I enable Flutter/Dart language experiments

2019-05-13 12:09发布

问题:

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)

回答1:

On master branch, you can create an analysis_options.yaml file in the root of your flutter app and write something like

analyzer:
  enable-experiment:
    - spread-collections

Saying all that, I took latest version of master (1.4.8-pre.41) and the spread operator compiles but it doesn't actually combine the values. I'm guessing it hasn't been fully implemented yet. For now...you can use the yield operator https://medium.com/flutter-community/quick-tip-sync-a-taste-of-the-future-9be4cd6993f4



回答2:

you are running an old version of flutter

spreading is available in flutter 1.5 and dart 2.3

Run flutter upgrade...