flutter - double http request when moving another

2019-08-19 07:14发布

I encountered a problem when I picked up the http request List which continued to increase when I went to the tab page and returned to its original page (see picture for result).

when the first build

then, after leaving another page and returning to the original

after leaving another page and returning to the original

how to optimize this?

see my code below:

class HomeTabDetail extends StatefulWidget {
  final Fixture fixture;
  final FixtureModel model;

  HomeTabDetail({this.fixture, this.model});

  @override
  _HomeTabDetailState createState() => _HomeTabDetailState();
}

class _HomeTabDetailState extends State<HomeTabDetail>
    with SingleTickerProviderStateMixin {
  TabController controller;
  // BannerAd bannerAd;

  @override
  void initState() {
    super.initState();
    loadData();
    print('initstate called');
    controller = TabController(vsync: this, length: 4);
    // FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
    // bannerAd = buildBanner()..load();
  }

  @override
  void dispose() {
    // bannerAd?.dispose();
    controller.dispose();
    print('dispose called');
    super.dispose();
  }

  static final targetingInfo = MobileAdTargetingInfo(
    keywords: <String>['flutterio', 'beautiful apps'],
    contentUrl: 'https://flutter.io',
    childDirected: false,
    testDevices: <String>[], // Android emulators are considered test devices
  );

  BannerAd buildBanner() {
    return BannerAd(
        adUnitId: BannerAd.testAdUnitId,
        size: AdSize.smartBanner,
        targetingInfo: targetingInfo,
        listener: (MobileAdEvent event) {
          print("bannerAd event is $event");
        });
  }

  Future loadData() async {
    await Future.wait([
      widget.model.fetchVenue(widget.fixture.venueId.toString()),
      widget.model.fetchCountry(widget.fixture.league.countryId.toString()),
      widget.model.fetchStandingTeams(widget.fixture.season.id.toString())
    ]);
  }

  @override
  Widget build(BuildContext context) {
    // bannerAd.show();
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('Info Pertandingan'),
          bottom: TabBar(
            controller: controller,
            tabs: <Widget>[
              Tab(text: 'RINCIAN'),
              Tab(text: 'MEDIA'),
              Tab(text: 'LINEUP'),
              Tab(text: 'KLASEMEN'),
            ],
          ),
        ),
        body: ScopedModelDescendant<FixtureModel>(
            builder: (context, child, model) {
          return TabBarView(
            controller: controller,
            children: <Widget>[
              DetailMatchTab(widget.fixture),
              MediaMatchTab(),
              LineupMatchTab(),
              KlasemenMatchTab()
            ],
          );
        }));
  }
}

here fetchStandingTeams() method

Future<List<StandingTeams>> fetchStandingTeams(String seasonId) async {
    var response =
        await http.get(Constant.standingById + seasonId + Constant.apiToken);
    var responseBody = json.decode(response.body)['data'];

    for (var standingJson in responseBody) {
      print(standingJson);

      for (var data in standingJson['standings']['data']) {
        var standings = StandingTeams.fromJson(data);
        print(standings);
        addToStandingList(standings);
      }
    }

    _isLoadingVenue = false;
    notifyListeners();

    return _standingTeams;
  }

0条回答
登录 后发表回答