如何防止重复出现在列表视图中?颤抖

时间:2020-05-23 03:13:00

标签: listview flutter dart duplicates mapping

所以我一直在试图找出如何停止将重复项添加到ListView的方法,但没有任何效果。请帮忙。将事件列表映射到位置列表图块时,我在列表视图中不断获得重复的位置。

这是我的代码:

@override
  Widget build(BuildContext context) {
    return Column(children: <Widget>[
      Expanded(
          child: ListView(
            padding: const EdgeInsets.fromLTRB(16.0, 0.0, 16.0, 8.0),
            controller: _scrollController,
            shrinkWrap: true,

                children: PlaceMapState.events
                .map((place) => _PlaceListTile(
                      place: place,
                      // onPlaceChanged: (value) => _onPlaceChanged(value),
                    ))
                .toSet().toList(),
          ),
        ),
    ],

    );

  }

}

这是我检索数据并将其放入事件列表的地方:

 void initPlace(request,requestId){
     var placeId = requestId;
   MarkerId placeIdVal = new MarkerId(placeId);
    //creating new Place
    final Place place = new Place(id: placeIdVal.toString(),

        latLng: LatLng(request['latLng'].latitude, request['latLng'].longitude),
        name: request['title'],
        category: AppState.of(context).selectedCategory,
      );

    setState((){
      events.add(place);
      events.toSet();
    places[placeIdVal]=place;
    print(place);
    });


  }

3 个答案:

答案 0 :(得分:0)

   @override
   bool operator == (other) {
    return this.id == other.id;
   }

   if (!events.contains(place)) {
     events.add(place);
    }

答案 1 :(得分:0)

<script lang="ts"> import { Vue, Component } from 'vue-property-decorator'; @Component({ name: 'signUpForm', }) export default class SignUpForm extends Vue { private valid = true private firstName = '' private lastName = '' private nameRules: Array<(v: string) => boolean | string> = [ (v) => !!v || 'Name is required', ] private email = '' private emailRules: Array<(v: string) => boolean | string> = [ (v) => !!v || 'E-mail is required', (v) => /.+@.+\..+/.test(v) || 'E-mail must be valid', ] } </script> 对象中覆盖==运算符

Place

并在添加之前检查列表中已经存在的项目

@override
  bool operator ==( other) {
  return  this.id==other.id;
  }

答案 2 :(得分:0)

您的if g == 'y': os.system('cls') # your code os.system('cls') # 4 space indent 无法正常工作,因为它在内部检查/匹配对象引用,因此您需要覆盖@Nidheesh所声明的==运算符,以便按对象的变量差异进行匹配。不要忘记添加字段(基元和类)。

.toSet().toList()

要加快实施速度,可以尝试使用Equatable库:https://pub.dev/packages/equatable