title: Flutter HTTP Request Called Multiple Times On App Load
body: <p>I have a flutter project that makes an http request to gather json of inventory items then render it on screen in a list view. When a user scrolls to the bottom it then loads more inventory by triggering another http call. However my initial HTTP call is being called multiple times. And I am not sure how to handle that.</p>
  <p>The result I get is my finish print statement just triggers continuously. At first I thought I had it working because it does load the inventory to my list view. But then I noticed It just keeps loading the same data into my list view non stop. When I added that finish print statement it became clear that it is actually continuously making that http call non stop.</p>
  <p>Id like to have it only make the call once, then make a new call again when the user scrolls to bottom.</p>
  <p>Any tips will help thank you.</p>
  <p>Here is my code.</p>
  <p>Network Code</p>
  <pre><code>Future &lt;List &lt;dynamic&gt;&gt; fetchInventory() async{
  dynamic response = await http.get('https:somelink.com',
    headers: {'x-api-key': 'mykey'},);
  
  var inventory = List&lt;dynamic&gt;();
  
  //if 200 response is given then set inventory var to inventoryJson value from the http call
  if (response.statusCode == 200){
    dynamic inventoryJson = jsonDecode(response.body);
    inventory = inventoryJson['page'];
    print('finish');
  }
  //Inventory is returned
      return inventory;
    }
  }
  </code></pre>
  <p>Here is my how I am using that code in my main file</p>
  <pre><code>class _WelcomeScreenState extends State&lt;WelcomeScreen&gt; {
  
    //Create Network Helper Obj
    NetworkHelper networkHelper = NetworkHelper();
    //invenrtory List is set to empty
    var inventory = [];
    var _controller = ScrollController();
    @override
    void initState() {
      super.initState();
  
      // set up listener here
      _controller.addListener(() {
        if (_controller.position.pixels == _controller.position.maxScrollExtent) {
          // Perform your task
          //This method fetches the data from the fetchInventory method
          print('at bottom');
          networkHelper.fetchInventory().then((value) {
  
            //This set state will set the value returned from fetchInventory method to the local inventory List
            setState(() {
              this.inventory = value;
            });
          });
        }
      });
    }
  
    @override
    Widget build(BuildContext context) {
      //Used to format currency shown in view
      var currencyFormat = NumberFormat('##,###.00', 'en_US');
  
      //This method fetches the data from the fetchInventory method
  
  
          networkHelper.fetchInventory().then((value){
            //This set state will set the value returned from fetchInventory method to the local inventory List
            setState(() {
              this.inventory.addAll(value);
            });
          });
  
  
      return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          elevation: 0.0,
          title: AppBarTitle(),
        ),
        body: Padding(
          padding: const EdgeInsets.only(left: 5.0, right: 5.0, bottom: 25.0),
          child: ListView.builder(
            controller: _controller,
            itemCount: inventory.length,
            itemBuilder: (context, index){
      enter code here
      enter code here
  </code></pre>