Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use spread operator in Flutter

Tags:

flutter

dart

I need to return a copy of the list _items.I tried using the spread operator by enclosing in square brackets like this " return [..._items]".I'm getting an error which says that spread collections experiment is not enabled,try enabling it. how can I enable that or is there any other way of returning a copy of a list?

Error -> "This requires the 'spread-collections' experiment to be enabled. Try enabling this experiment by adding it to the command line when compiling and running.".

I have attached an image that clearly depicts the error ,below.

enter image description here

import 'package:flutter/material.dart';

import '../models/product.dart';

class ProductProvider with ChangeNotifier {
  List<Product> _items = [];

  List<Product> get items {
    return [..._items];// I'm getting error here.

  }

  void addProduct() {
    notifyListeners();
  }
}
like image 625
Rishikrishna Avatar asked Jan 26 '23 11:01

Rishikrishna


1 Answers

You are using an older version of Dart.

You can fix thix by updating your SDK constraint in pubspec.yaml:

environment:
  sdk: ">=2.7.0 <3.0.0"
like image 98
creativecreatorormaybenot Avatar answered Jan 27 '23 23:01

creativecreatorormaybenot