I am using firebase for the data querying. I am using a pagedlistview and a controller in order to create an infinite list. And this list generates items, that can be filtered through a filter. The filter option updates the app state, and what I am trying to do is make the listview listen to the app state, then filter the data based off it, and refresh with the new data once the filter has been selected, so it shows the new items that have been filtered through the options.
Right now it is only filtering due to MaxNum due to me testing it. It all loads whenever no filters are applied. But when anything is filtered, then it returns no items or an error. Whenever I try to fix the code, there still happen to be errors. I have been working on this for around 2 weeks and still don't manage to have a solution ๐ Here is the code:
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import '/flutter_flow/flutter_flow_icon_button.dart';
import '/flutter_flow/flutter_flow_widgets.dart';
import '/home/ventures/ventures_widget.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class FilteredVenturesListView extends StatefulWidget {
const FilteredVenturesListView({
Key? key,
this.width,
this.height,
}) : super(key: key);
final double? width;
final double? height;
@override
State<FilteredVenturesListView> createState() =>
_FilteredVenturesListViewState();
}
class _FilteredVenturesListViewState extends State<FilteredVenturesListView> {
final PagingController<int, VenturesRecord> _pagingController =
PagingController(firstPageKey: 0);
final int _numberOfVenturesPerRequest = 10;
bool _isLoading = false;
final appState = FFAppState();
void _listenForAppStateChanges() {
appState.addListener(() {
_pagingController.refresh();
});
}
@override
void initState() {
super.initState();
_listenForAppStateChanges();
_pagingController.addPageRequestListener((pageKey) {
_fetchPage(pageKey);
});
}
@override
void dispose() {
_pagingController.dispose();
_listenForAppStateChanges();
super.dispose();
}
void _refreshListView() {
_pagingController.refresh();
// _fetchPage(0);
}
Future<void> _fetchPage(int pageKey) async {
if (_isLoading) {
return; // Already loading, do nothing
}
_isLoading = true;
Query? venturesQuery; // Declare venturesQuery here
if (appState.maxPeople == -1) {
// Check for -1 (unlimited)
final venturesQuery = FirebaseFirestore.instance
.collection('ventures')
.orderBy('created_time')
.limit(_numberOfVenturesPerRequest);
} else {
final venturesQuery = FirebaseFirestore.instance
.collection('ventures')
.where("maxNum",
isEqualTo: appState.maxPeople) // Corrected field name
.orderBy('created_time')
.limit(_numberOfVenturesPerRequest);
}
if (pageKey > 0) {
final lastDocumentSnapshot =
await _pagingController.itemList?.last?.reference.get();
if (lastDocumentSnapshot != null && lastDocumentSnapshot.data() != null) {
final lastCreatedTime = lastDocumentSnapshot.get('created_time');
venturesQuery?.startAfter([lastCreatedTime]);
}
_isLoading = false;
}
final venturesSnapshot = await venturesQuery?.get();
final OldVenturesList = venturesSnapshot?.docs
.map((doc) => VenturesRecord.fromSnapshot(doc))
.toList() ??
[];
final venturesList = OldVenturesList?.where((venture) {
return ((appState.maxPeople == -1 ||
venture.maxNum == appState.maxPeople) &&
(appState.ventureCountries?.isEmpty ??
true ||
appState.ventureCountries!.contains(venture.destination)) &&
(appState.ventureIndustries?.isEmpty ??
true || appState.ventureIndustries!.contains(venture.industry)) &&
(appState.ventureMonth == 'Empty' ||
venture.startingMonth == appState.ventureMonth) &&
(appState.minWeeks == -1 || venture.minLength >= appState.minWeeks) &&
(appState.maxWeeks == -1 || venture.maxLength <= appState.maxWeeks));
// return true;
}).toList();
final isLastPage = venturesList?.length ?? 0 < _numberOfVenturesPerRequest;
if (isLastPage) {
_pagingController?.appendLastPage(venturesList);
} else {
final nextPageKey = pageKey + 1;
_pagingController?.appendPage(venturesList, nextPageKey);
}
}
@override
Widget build(BuildContext context) {
return PagedListView<int, VenturesRecord>(
pagingController: _pagingController,
builderDelegate: PagedChildBuilderDelegate<VenturesRecord>(
itemBuilder: (context, venture, index) {
return VenturesWidget(
key: Key(
'Keyqg1_${index}_of_${_pagingController.itemList?.length ?? 0}'),
ventureRef: venture.reference,
userRef: venture.creator!,
);
},
),
);
}
}