'Creating pageview with dynamic number of pages in flutter

Please find the code I have created. The problem here I face is the third item is getting called thrise when I increment the item count of the page view. Please let me know if there is any fix for this. I think this happens due to setState rebuild the widget. If I comment the setState it will work but dynamic page creation doesn't work. Let me know the fix for this.

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../sub_screens/single_article_item.dart';
import '../sub_screens/two_article_item.dart';
import '../sub_screens/three_article_item.dart';

class ArticleScreen extends StatefulWidget {
  @override
  _ArticleScreenState createState() => _ArticleScreenState();
}

class _ArticleScreenState extends State<ArticleScreen> {
  var homeDataList = List<Home>();
  int _pageNo = 3;

  void setPageNo(int page) {
    setState(() {
      _pageNo = page;
    });

    //notifyListeners();
  }

  @override
  Widget build(BuildContext context) {
    final vm = Provider.of<HomeViewModel>(context, listen: false);
    homeDataList.add(Home("First Article Screen"));
    homeDataList.add(Home("Second Article Screen"));
    homeDataList.add(Home("Other type Article Screen"));

    PageController controller = PageController(
      keepPage: false,
      initialPage: 0,
    );
    var currentPageValue = 0.0;

//     return ChangeNotifierProvider(create: (ctx)=> LocationProvider(),child:MaterialApp(
//       home: Scaffold(body: Container(child: LocationScreen(),)),
//     ));
//   }
// }

    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          body: Container(
            child: PageView.builder(
              controller: controller,
              itemBuilder: (context, position) {
                if (position == 0) {
                  //vm.fetchHomeData(":::third page");
                  print('first article');
                  return SingleArticleItem();
                } else if (position == 1) {
                  print('two article');
                  return TwoArticleItem();
                } else {
                  print("third article " +
                      _pageNo.toString() +
                      "::" +
                      position.toString());
                  return ThreeArticleItem(vm.getPageNo, vm.getHomeData);
                }
              },
              onPageChanged: (index) {
                if (index >= 1) {
                  //  print('onPageChanged - two article');
                  controller.addListener(() {
                    setState(() {
                      currentPageValue = controller.page;
                      _pageNo = _pageNo + 1;
                      //  print('onPageChanged - three article');
                    });
                  });
                }
              },
              // itemCount: vm.getPageNo,
              itemCount: vm.getPageNo,
              scrollDirection: Axis.vertical,
            ),
          ),
        ),
      ),
    );
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source