Home > Enterprise >  how to filter JSON data in Flutter?
how to filter JSON data in Flutter?

Time:08-14

I have 2 methods to read radio station data: _readStations and _getStationsRemoteJson

I can filter stations within Firestore data in _readStations method as shown in the following code snippet.

But I cannot do the same filtering with JSON data.

How can I filter JSON data?

Future<List<Station50K>> _getStationsRemoteJson() async {
  const url = 'JSON URL'; 

  final response = await http.get(Uri.parse(url));
  final body = json.decode(response.body);
  final stations = body['data'];

  return stations.map<Station50K>(Station50K.fromJson2).toList();
}

Stream<List<Station>> _readStations() {
  return FirebaseFirestore.instance.collection('stations').snapshots().map(
      (snapshot) => snapshot.docs
          .where((d) =>
              (_favorites.contains(d.data()['id'].toString())) &&
              (d
                      .data()['title']
                      .toString()
                      .toLowerCase()
                      .contains(_searchText.toLowerCase()) ||
                  d
                      .data()['city']
                      .toString()
                      .toLowerCase()
                      .contains(_searchText.toLowerCase()) ||
                  List.from(d.data()['genre']).any((pattern) {
                    return pattern
                        .toString()
                        .toLowerCase()
                        .contains(_searchText.toLowerCase());
                  })))
          .map((doc) => Station.fromJson2(doc.data()))
          .toList());
}

CodePudding user response:

In _getStationsRemoteJson() when returning a list of objects you can first filter it (same as what you have for Firestore snapshot.docs.where(...)).

It would look like stations.map<Station50K>(Station50K.fromJson2).where((object) => object.property == 'searchSomething'). Probably what's confusing you is that from Firestore, the data you get back is a Map, while here you have already converted it to an object so you can use the object's properties.

Also, for filtering Firestore data there are specific built in methods so you get less data back from server and it costs you a lot less money if used often.Check it out here.

CodePudding user response:

I think I solved it.

Future<List<Station50K>> _getStationsRemoteJson() async {
const url = 'JSON URL';

final response = await http.get(Uri.parse(url));
final body = json.decode(response.body);
final stations = body['data'];

List<Station50K> list = stations.map<Station50K>(Station50K.fromJson2).toList();

return list
    .where((d) =>
        (_favorites.contains(d.id)) &&
        (d.name.toLowerCase().contains(_searchText.toLowerCase()) ||
            d.city.toLowerCase().contains(_searchText.toLowerCase()) ||
            List.from(d.genres).any((pattern) {
              return pattern
                  .toString()
                  .toLowerCase()
                  .contains(_searchText.toLowerCase());
            })))
    .toList();
}
  • Related