本文介绍了可选参数的默认值必须为常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了这个Event Tracker应用程序,我有两个屏幕,分别是地图和事件列表。
我正在尝试使地点清单等于我在App状态下的地点。切记placeList是可修改的列表,我需要在该列表中添加位置。

So Im creating this Event Tracker app and I have two screens which are the map and the events list.I am trying to get the place list to be equal to my places in my App state. Bare in mind that placeList is a modifiable list that I need to add places to this list.

但是,每次初始化 this.places = PlaceMapState.placeList 时,我都会得到可选参数的默认值必须为常数 ,我无法将其更改为常量,因为我需要它来更新PlaceMapState类中的位置列表,并且无法从AppState中删除它,因为我正在PlaceList类中使用它来获取位置作为列表

However I am getting a "The default value of an optional parameter must be constant" whenever I initialize this.places=PlaceMapState.placeList and I cant change it to a constant since i need it to update my list of places in the PlaceMapState class and I cant remove it from AppState since I am using it in the PlaceList class to get the places as a list.

我也不想完全删除AppState,因为它也包含地图。

I also dont want to remove the AppState entirely because it also contains the map.

请对此问题采取任何解决方案?

Please any solution to this???

这是我上课的地方,使用此列表:

Here is my class where I use this list:

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的类,我在其中使用位置来获取列表:

Here is the class of placeList where I use places to get a list:

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(),
          ),
        ),
      ],
    );
  }
}


推荐答案

A对默认参数要求常量值的常见解决方法是改为提供 sentinel 参数作为默认值,即可以 const 。通常,前哨参数可以为 null

A common workaround to requiring constant values for default arguments is to instead provide a sentinel argument as the default that can be const. Typically that sentinel argument can be null:

class AppState {
   AppState({
    List<Place> places,
    ...
   }) : places = places ?? PlaceMapState.placeList,
        assert(places != null),
        assert(selectedCategory != null);

这篇关于可选参数的默认值必须为常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 02:24