list - 可选参数的默认值必须是常量
问题描述
所以我创建了这个 Event Tracker 应用程序,我有两个屏幕,分别是地图和事件列表。我正在尝试使位置列表与我在应用程序状态中的位置相同。请记住 placeList 是一个可修改的列表,我需要将地点添加到此列表中。
但是,每当我初始化时,我都会收到“可选参数的默认值必须是常量” this.places=PlaceMapState.placeList
,并且我无法将其更改为常量,因为我需要它来更新 PlaceMapState 类中的位置列表,并且我无法将其从 AppState 中删除我在 PlaceList 类中使用它来获取地点作为列表。
我也不想完全删除 AppState,因为它还包含地图。
请问这个有什么解决办法???
这是我使用此列表的班级:
class AppState {
AppState({
this.places = PlaceMapState.placeList, //// here is the problem!!!!!!!!!!!!!!!!!!!
this.selectedCategory = PlaceCategory.events,
this.viewType = PlaceTrackerViewType.map,
}) : //assert(places != null),
assert(selectedCategory != null);
List<Place> places;
PlaceCategory selectedCategory;
PlaceTrackerViewType viewType;
AppState copyWith({
List<Place> places,
PlaceCategory selectedCategory,
PlaceTrackerViewType viewType,
}) {
return AppState(
selectedCategory: selectedCategory ?? this.selectedCategory,
viewType: viewType ?? this.viewType,
);
}
static AppState of(BuildContext context) => AppModel.of<AppState>(context);
static void update(BuildContext context, AppState newState) {
AppModel.update<AppState>(context, newState);
}
static void updateWith(
BuildContext context, {
List<Place> places,
PlaceCategory selectedCategory,
PlaceTrackerViewType viewType,
}) {
update(
context,
AppState.of(context).copyWith(
places: places,
selectedCategory: selectedCategory,
viewType: viewType,
),
);
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other.runtimeType != runtimeType) return false;
return other is AppState &&
other.places == places &&
other.selectedCategory == selectedCategory &&
other.viewType == viewType;
}
@override
int get hashCode => hashValues(places, selectedCategory, viewType);
}
下面是 placeList 类,我使用地点来获取列表:
class PlaceList extends StatefulWidget {
const PlaceList({Key key}) : super(key: key);
@override
PlaceListState createState() => PlaceListState();
}
class PlaceListState extends State<PlaceList> {
ScrollController _scrollController = ScrollController();
void _onCategoryChanged(PlaceCategory newCategory) {
_scrollController.jumpTo(0.0);
AppState.updateWith(context, selectedCategory: newCategory);
}
void _onPlaceChanged(Place value) {
// Replace the place with the modified version.
final newPlaces = List<Place>.from(AppState.of(context).places);
final index = newPlaces.indexWhere((place) => place.id == value.id);
newPlaces[index] = value;
AppState.updateWith(context, places: newPlaces);
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
_ListCategoryButtonBar(
selectedCategory: AppState.of(context).selectedCategory,
onCategoryChanged: (value) => _onCategoryChanged(value),
),
Expanded(
child: ListView(
padding: const EdgeInsets.fromLTRB(16.0, 0.0, 16.0, 8.0),
controller: _scrollController,
shrinkWrap: true,
children: AppState.of(context)
.places //this the places im talking about!!!!!!!!!!!!!!!!!
.where((place) =>
place.category == AppState.of(context).selectedCategory)
.map((place) => _PlaceListTile(
place: place,
onPlaceChanged: (value) => _onPlaceChanged(value),
))
.toList(),
),
),
],
);
}
}
解决方案
要求默认参数的常量值的常见解决方法是使用哨兵参数作为默认值,可以是const
. 通常,哨兵参数可以是null
:
class AppState {
AppState({
List<Place>? places,
...
}) : places = places ?? PlaceMapState.placeList;
List<Place> places;
...
}
推荐阅读
- python-3.x - Bitbucket:ModuleNotFoundError:没有名为“numpy”的模块
- mysql - 对于这个复杂的数据,查询应该是什么样子?
- discord - 从 webhook 编辑不和谐消息
- r - 使用 RJDBC 访问 Redshift 时如何使用 AWS 命名配置文件
- javascript - 如何处理 react-query 的 onSuccess 事件中的错误?
- html - 反应部署后 Netlify 出现 404 错误
- sql-server - 保存 MSSQL docker 容器数据的最佳方法是什么?
- python - 通过另外两列更新熊猫
- vue.js - 如何使用 Vue.js 和 Tailwind 有条件地将多个类添加到基于数组中的元素?
- asp.net - 添加中间件后无法连接到 Asp.net API