小编Oma*_*ajj的帖子

可选参数的默认值必须是常量

所以我正在创建这个事件跟踪应用程序,我有两个屏幕,即地图和事件列表。我试图让地点列表等于我在应用程序状态中的地点。请记住,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 …
Run Code Online (Sandbox Code Playgroud)

constants list optional-parameters dart flutter

1
推荐指数
1
解决办法
5256
查看次数

标签 统计

constants ×1

dart ×1

flutter ×1

list ×1

optional-parameters ×1