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

问题描述 投票:0回答:1

所以,我创建了这个事件跟踪应用程序,我有两个屏幕,这是地图和事件列表.我试图让地方列表等于我的应用程序状态下的地方。请记住,placeList是一个可修改的列表,我需要添加地方到这个列表。

然而,每当我初始化一个可选参数的默认值必须是常数时,我都会得到一个 "The default value of an optional parameter must be constant "的信息。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);
}

这是我用地方列表的类,我用地方来得到一个列表。

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(),
          ),
        ),
      ],
    );
  }
}
list flutter dart constants optional-parameters
1个回答
0
投票

要求默认参数的常量值的一个常见的变通方法是使默认参数的值为 null 然后再进行分配。 你能不能做到。

class AppState {
   AppState({
    List<Place> places,
    ...
   }) : places = places ?? PlaceMapState.placeList,
        assert(places != null),
        assert(selectedCategory != null);
© www.soinside.com 2019 - 2024. All rights reserved.