我是Flutter和Riverpod新手。我试图存档的是从服务器上获取的项目列表,带有分页和刷新列表选项。分页工作良好,但刷新工作出于某种原因,只有当_page> 1,否则部件不被重建。我现在想弄清楚几个小时了。我可以看到DriverRepository().driverJobList(tab,pages,filtering);被调用,新的数据从JSON中出现。
import 'package:super_carga_mobileapp/styles.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../Driver/Jobs/model.dart';
import 'package:super_carga_mobileapp/utilis.dart';
import 'job_list_provider.dart';
import 'widgets/job_list_element.dart';
class JobListPage extends ConsumerWidget {
List<JobListProperties> jobPropertiesList = [];
late int _maxPages;
int _page =1;
@override
Widget build(BuildContext context, WidgetRef ref) {
late ScrollController _scrollController;
final jobListAsyncValue = ref.watch(JobListResponseProvider);
void _loadMore() {
if (_scrollController.position.atEdge) {
if (_scrollController.position.extentAfter < 500) {
if (_page < _maxPages) {
ref.read(JobListPaginationProvider.notifier).state = ++_page;
}
}
}
}
_scrollController = ScrollController()..addListener(_loadMore);
Widget _buildList(int length) {
return RefreshIndicator(
onRefresh: () async {
jobPropertiesList.clear();
if (_page > 1) {
_page = 1;
ref.read(JobListPaginationProvider.notifier).state = _page;
} else {
return await ref.refresh(JobListResponseProvider);
}
},
child: Column(
children: [
Expanded(
child: ListView.separated(
controller: _scrollController,
itemCount: length,
itemBuilder: (context, index) {
return JobListElement(jobPropertiesList[index]);
},
separatorBuilder: (BuildContext context, int index) =>
const Seperator(),
),
),
],
),
);
}
return jobListAsyncValue.when(
data: (data) {
if (data.data.isNotEmpty) {
jobPropertiesList.addAll(data.data.toList());
final length = jobPropertiesList.length;
_maxPages = data.pages;
return _buildList(length);
} else {
return Column(
children: [
SizedBox(
height: 60,
),
Text(
"No results",
style: titleStyle,
),
],
);
}
},
error: (error, stackTrace) => ErrorMessage(message: error.toString()),
loading: () {
if (jobPropertiesList.isNotEmpty) {
return _buildList(jobPropertiesList.length);
} else {
return PageLoadingSpinnerWidget();
}
},
);
}
}
final JobListResponseProvider = FutureProvider.autoDispose<JobListResponse>((ref) async {
final pages = ref.watch(JobListPaginationProvider);
final tab = ref.watch(MiddleScreenTabProvider);
final filtering = ref.watch(searchTextProvider);
return await DriverRepository().driverJobList(tab, pages, filtering);
});
final JobListPaginationProvider = StateProvider.autoDispose<int>((ref) {
return 1;
});
final searchTextProvider = StateProvider<Filtering>((ref) {
return Filtering("");
});
final bottomNavIndex = StateProvider<int>((ref) {
return 0;
});
final DeliveryButtonProvider = StateProvider<bool>((ref) {
return false;
});
final PickupButtonProvider = StateProvider<bool>((ref) {
return false;
});
final MiddleScreenTabProvider = StateProvider<int>((ref) {
return 1;
});
1条答案
按热度按时间qnakjoqk1#
好了,现在我看到我的列表被加倍了,所以当_page == 1并且我刷新列表时,列表被相同的值颠倒了。它没有被清除,然后加载值