I'm facing an issue with filtering data in my Flutter app using the Provider package. I have a list of items that I want to filter based on a selected category. However, the filtering doesn't seem to work as expected.
Here's the relevant code snippet:
import 'dart:convert';
import 'package:appwrite/appwrite.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class DataProvider with ChangeNotifier {
final Databases database;
final String collectionId;
List<Map<String, dynamic>> items = [];
List<Map<String, dynamic>> filteredItems = [];
List<String> categories = [
'Cat 1',
'Cat 2',
'Cat 3',
'Cat 4',
'Cat 5',
'Cat 6'
];
int selectedCategoryIndex = 0;
int currentPage = 1;
SharedPreferences? prefs;
DataProvider({required this.database, required this.collectionId}) {
init();
}
Future<void> init() async {
prefs = await SharedPreferences.getInstance();
await loadDataFromPrefs();
await fetchData();
}
Future<void> saveDataToPrefs() async {
final stringList = items.map((item) => json.encode(item)).toList();
await prefs?.setStringList('items', stringList);
}
Future<void> loadDataFromPrefs() async {
final savedData = prefs?.getStringList('items');
if (savedData != null) {
items = savedData
.map((item) => json.decode(item))
.cast<Map<String, dynamic>>()
.toList();
filterItemsByCategory(categories[selectedCategoryIndex]);
notifyListeners();
}
}
void filterItemsByCategory(String category) {
filteredItems = items
.where(
(item) => item['category'].toLowerCase() == category.toLowerCase())
.toList();
}
void setSelectedCategoryIndex(int index) async {
selectedCategoryIndex = index;
await fetchData();
}
Future<void> fetchData() async {
print('Fetching data');
final category = categories[selectedCategoryIndex];
print('Selected category: $category');
// Indien een specifieke categorie is geselecteerd (d.w.z. niet 'Algemeen'), voeg een filter toe
Map<String, dynamic> filters = {};
if (selectedCategoryIndex != 0) {
filters = {
'category': {
'\$eq': category,
},
};
}
final response = await database.listDocuments(
collectionId: 'posts',
databaseId: 'app',
);
final documents = response.documents;
items.clear();
items.addAll(documents.map((doc) {
final decodedTitle = utf8.decode(base64.decode(doc.data['title']));
final decodedContent = utf8.decode(base64.decode(doc.data['content']));
return {
...doc.data,
'title': decodedTitle,
'content': decodedContent,
};
}));
print('Items: $items');
currentPage++;
await saveDataToPrefs();
// Update filteredItems after fetching data
filterItemsByCategory(category);
notifyListeners(); // Notify listeners after updating filteredItems
}
}
In my NewsScreen widget, I'm using the DataProvider to display and filter the items based on the selected category. However, when I select a category, the filtering doesn't seem to have any effect on the displayed items. The filteredItems list remains empty.
I have verified that the items list has the correct data and the filterItemsByCategory method is being called with the correct category value. But for some reason, the filtering doesn't work.
Am I missing something in the implementation? Is there a better way to filter the items using Provider?
Any help or guidance would be greatly appreciated. Thank you!