flutter - Unable to use spread operator in Flutter
问题描述
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.
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();
}
}
解决方案
你使用的是旧版本的 Dart。
您可以通过在以下位置更新您的 SDK 约束来修复 thix pubspec.yaml
:
environment:
sdk: ">=2.7.0 <3.0.0"
推荐阅读
- python - 使用索引切片打印字符串中的每个单词
- docker - 从正在运行的容器创建 docker 映像
- javascript - 如何导出多个融合图以分离图像文件
- python - 如何在 Django 中从 views.py 中导入变量?
- google-my-business-api - 任何 GMB 位置的问答部分是否有任何预览 URL...?
- spring-boot - Java Spring Boot:使独立应用程序无需退出即可运行
- php - 如何只显示一次结果检查的消息
- java - 春季启动:java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
- json - json中特定变量后的jinja匹配字符串
- android - [Android][RecyclerView] 行可见时回调