【发布时间】:2020-09-03 13:46:49
【问题描述】:
我正在开发一个直播电视应用程序。它有一个垂直列表视图,其中包含许多细分。每个细分都有一个水平列表视图。这是应用程序的外观:https://youtu.be/R023PN289RM
问题是我必须对每个数据使用 API。让它成为个人资料图片、标题和副标题,对于我正在加载数据并将其复制到一个数组中的每一件事,我正在显示它.所以这是我正在做的非常重复的事情,我觉得它不好。此外,该应用程序滞后,因为它必须加载大量数据,我只是想知道有没有办法可以改善这一点:
我解决这个问题的方式是针对给定的 json(https://docs.google.com/document/d/1saJN3MQvG55M1ipf42-65Etowi_kW80gkrosU6vBb5o/edit?usp=sharing)
我已经制作了一个 PODO 文件,这里是https://docs.google.com/document/d/1un989A1xn4mo5wz-7KrQTSwQJGkJihKpAqdlZSbaQPE/edit?usp=sharing
然后我做了一个服务文件来加载数据。为了简单起见,我只包括了两个函数,否则它会变得很长
class Services {
static const String url =
"https://livetvapi.apyhi.com/api/v3/home?pageLocation=home&countries=IN&app_version=13&"
"user_id=44edc2c905ae163f&package_id=livetv.movies.freemovies.watchtv.tvshows&os_platform=android";
static Future<List<String>> loadDataForMovieId() async {
var res = await http
.get(url, headers: {'Authorization': dartJsonWebTokenGenerator()});
if (res.statusCode == 200) {
print("response is there");
final homePage = homePageFromJson(res.body);
print("homeBannerlist is there");
print("response .............");
print(res.body);
// print (homePage.homeBanners);
HomeBanner homeBannerObject = new HomeBanner();
List<String> homeBannerObjectMovieIdList = [];
for (homeBannerObject in homePage.homeBanners)
homeBannerObjectMovieIdList.add(homeBannerObject.movieId);
print("Movie ID list is there");
print(homeBannerObjectMovieIdList);
return homeBannerObjectMovieIdList;
} else {
print("no response");
return null;
}
}
static Future<List<String>> loadDataForMovieIdofPopularMovieSection() async {
var res = await http
.get(url, headers: {'Authorization': dartJsonWebTokenGenerator()});
if (res.statusCode == 200) {
print("response is there");
final homePage = homePageFromJson(res.body);
HomeBanner homeBannerObject = new HomeBanner();
List<String> homePageObjectPopularMovieID = [];
for (homeBannerObject in homePage.movies)
homePageObjectPopularMovieID.add(homeBannerObject.movieId);
print("Movie ID list is there");
print(homePageObjectPopularMovieID);
return homePageObjectPopularMovieID;
} else {
print("no response");
return null;
}
}
有 10-15 个这样的加载函数来加载不同的元素,如下所示:
然后在初始化状态下我是这样加载的
@override
void initState() {
// TODO: implement initState
super.initState();
handleScroll(); // function which is responsible for updating the isScrollingDown variable whenever the user scrolls down
Services.loadDataForMovieId().then((movieIdList) {
setState(() {
_homeBannerObjectMovieIdList = movieIdList;
});
});
Services.loadDataForMovieIdofPopularMovieSection().then((movieIdList) {
setState(() {
_popularMoviesMovieId = movieIdList;
});
});
Services.loadDataForPopularTvShowSection().then((homePageSeriesPosterList) {
setState(() {
_seriesData = homePageSeriesPosterList;
});
});
像这样在init状态函数中有10-15个Services.loadDataFor..........函数
然后在 listView 或 GridView 构建器中,我使用这些数组来显示图片、标题、子标题和其他内容。
它工作正常,但是加载需要很多时间。而且正如您所见,事情非常重复。您如何真正编写简洁的代码并提高性能?
【问题讨论】:
标签: performance flutter dart