PostsGalleryProvider.dart 1.6 KB

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