PostsGalleryProvider.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:douyin_demo/models/PostsModel.dart';
  4. import 'package:douyin_demo/widgets/WebRequest.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:http/http.dart' as http;
  7. import 'package:provider/provider.dart';
  8. class PostsGalleryProvider with ChangeNotifier {
  9. List<PostsModel> model1 = List<PostsModel>();
  10. List<PostsModel> model2 = List<PostsModel>();
  11. double _len1 = 0;
  12. double _len2 = 0;
  13. List<PostsModel> posts = List<PostsModel>();
  14. PostsGalleryProvider() {
  15. getPosts(0, 0);
  16. // notifyListeners();
  17. }
  18. dispose(){
  19. super.dispose();
  20. }
  21. Future getPosts(orderType, ifRefresh) async {
  22. Uri url = await WebRequest().generate('posts/getPosts', {
  23. "openId": "ol_BV4zcyVJaOBtOTD5AfpkFERww",
  24. "dataFrom": "0",
  25. "count": "30",
  26. "refreshTime": DateTime.now().toString(),
  27. "currentSel": "1",
  28. "ulo": "0",
  29. "ula": "0"
  30. });
  31. var response = await http.get(url);
  32. //.then((response) {
  33. // var post = json.decode(response.body)["result"];
  34. setGalleryModel(response.body);
  35. notifyListeners();
  36. }
  37. setGalleryModel(String items) {
  38. var result = List<PostsModel>();
  39. var posts = json.decode(items)["result"];
  40. // result.add(posts);
  41. for (var item in posts) {
  42. result.add(PostsModel.fromJson(item));
  43. }
  44. for (var item in result) {
  45. if (_len1 <= _len2) {
  46. item.makerName="";
  47. item.picsPath="";
  48. item.postsLocation="";
  49. item.postsReaded=0;
  50. model1.add(item);
  51. _len1 += item.picsRate;
  52. } else {
  53. model2.add(item);
  54. _len2 += item.picsRate;
  55. }
  56. }
  57. // notifyListeners();
  58. }
  59. }