Home > Net >  How to remove an element from a http call? Flutter/Dart
How to remove an element from a http call? Flutter/Dart

Time:09-09

hello I am doing a http request and I want to remove the elements which have some specific value, for example if 'region' is 'USA' I don't want to receive it.

here is my call:

  Future fetch() async {
    if (isLoading) return;
    isLoading = true;

    const limit = 10;
    final url = Uri.parse('https://everywhere-dev.iccs.gr:18083/evse/getEvsesPaged?page=$page&pageLimit=$limit&lat='  
        (this.widget.appController.currentLocation != null ?  this.widget.appController.currentLocation!.latitude : this.widget.appController.originalMapPoint.latitude).toString()   '&lon='  
        (this.widget.appController.currentLocation != null ?  this.widget.appController.currentLocation!.longitude : this.widget.appController.originalMapPoint.longitude).toString());
    final response = await http.get(url);
    if (response.statusCode == 200) {
      final List newItems = json.decode(response.body)["results"];

      setState(() {
        page  ;
        isLoading = false;

        if (newItems.length <limit) {
          hasMore = false;
        }
        tempEvses.addAll(newItems.map<Evse>((items) {
          final evseID = items['evseID'];
          final friendlyName = items['friendlyName'];
          final street = items['street'];
          final streetNumber = items['streetNumber'];
          final region = items['region'];
          final lat = items['lat'] ?? 0.0;
          final lng = items['lng'] ?? 0.0;
          final connectorList = items['connectorList'];
          final cpSerial = items['cpSerial'];
          final img = items['img'];


          return new Evse(evseID: evseID, friendlyName: friendlyName, street: street, streetNumber: streetNumber, region: region, lat: lat, lng: lng, connectorList: [connectorList], cpSerial: cpSerial, img: img);


        }).toList());
        for(int i=0 ; i<tempEvses.length; i    ){
          this.tempEvses[i].calculateDistance(widget.appController.currentLocation);
        //  this.tempEvses[i].calculateAvailability();
          this.tempEvses[i].isEvseFavorite(widget.appController.favoritesList);
          this.tempEvses[i].storePlugCounters();
        }
        showEvsePanels();
      });
    }
  }

CodePudding user response:

If you don't want to receive certain values you must work on the API and not on the app.
Maybe you couldn't work on the API so the only way is to work on the data that your app received.

You should try newItems.removeWhere((item) => item['region'] == 'USA')

CodePudding user response:

Change you map to this:

tempEvses.addAll(
  newItems.map<Evse>(
    (items) {
      final evseID = items['evseID'];
      final friendlyName = items['friendlyName'];
      final street = items['street'];
      final streetNumber = items['streetNumber'];
      final region = items['region'];
      final lat = items['lat'] ?? 0.0;
      final lng = items['lng'] ?? 0.0;
      final connectorList = items['connectorList'];
      final cpSerial = items['cpSerial'];
      final img = items['img'];

      if (region != 'USA') {
        return new Evse(
          evseID: evseID,
          friendlyName: friendlyName,
          street: street,
          streetNumber: streetNumber,
          region: region,
          lat: lat,
          lng: lng,
          connectorList: [connectorList],
          cpSerial: cpSerial,
          img: img);
      }
    },
  ).toList(),
);
  • Related