rate us
rate us

Reputation: 3

Query map object data in dart/flutter based on key value

I have following API response from the server

{
_embedded: [
    {
    _id: {
        $oid: "5e26c4d32c9da790ea1c4b7a"
    },
    storeName: "ggagav",
    phone: "+919654846950",
    pincode: "ahhahbba",
    area: "qhhahs",
    city: "whahhw",
    state: "whahhw",
    _etag: {
        $oid: "5e26c4d3462ceb46c79b7439"
    }
    },
    {
    _id: {
        $oid: "5e26c48f2c9da790ea1c4b67"
    },
    storeName: "gggg",
    phone: "+919654846440",
    pincode: "161161",
    area: "gvqvvavav",
    city: "vavavav",
    state: "qvavava",
    _etag: {
        $oid: "5e26c48f462ceb46c79b7438"
    }
    },
    {
    _id: {
        $oid: "5e26c1152c9da790ea1c4add"
    },
    storeName: "rajprakash",
    phone: "+919654846230",
    pincode: "shshsbs",
    area: "hshsbsba",
    city: "ahahshhs",
    state: "shshsbs",
    _etag: {
        $oid: "5e26c115462ceb46c79b7437"
    }
    }

],
_id: "stores",
_returned: 5
}

i want to know how to get the oid of any element when phone number is provided in the function.

I am able to get the Api information with the following request

 Future<Stores> getStore() async {
    try {
      Response response = await _dio.get(_endPoint, options: Options(
        headers: <String, String>{'authorization': _b},
      )
      );
      if(response.statusCode == 200){
        print(response.data);
      return Stores.fromJson(response.data);}else{return null;}
    } catch (error, stacktrace) {
      print("Exception occured: $error stackTrace: $stacktrace");
      return (error);
    }
  }

and I want to display only the store which has a selected phone number

Future <Stores> getStoresData() async{
    final r = await _repository.getStores();
    return r;
}

Here r is providing the whole response . how can i select a particular store from the response data.

Upvotes: 0

Views: 2851

Answers (1)

Antonio Valentic
Antonio Valentic

Reputation: 370

You should create a class for just one Store and add all stores from JSON array to a variable of type List. Then you could use the following code.

String getOidFromPhoneNumber(List<Store> stores, String phoneNumber) {
  Store store = stores.firstWhere((store) => store.number == phoneNumber);

  return store.id ?? "No store matching with that phone number";
}

Upvotes: 2

Related Questions