Have to reload the app, when the button was clicked to see changes (Flutter)

1,175

You can use setState() with StatefulWidget to rebuild the widget once the new images come.

Share:
1,175
Ostap Filipenko
Author by

Ostap Filipenko

Updated on December 21, 2022

Comments

  • Ostap Filipenko
    Ostap Filipenko over 1 year

    This is my project: https://github.com/OstapFilipenko/Picslash

    I trying to create an app, that is getting pictures from the unsplash API and display those pictures. First am calling an async function to get all images and to save them in List. I am using ListView.builder to display them, and on the end of the listView i have a Button that makes a API call and adding new Pictures to ListView.

    The function:

    var newImages = new List<Picture>();
      _getNewPics() async{
      counter++;
      newImages.clear();
      newImages = await API_connection(counter).getPictures();
      allImages.addAll(newImages);
    }  
    

    And here is the button:

    child: FlatButton(
                  child: Text("Load More",
                          style: new TextStyle(
                            color: Colors.white
                          )
                        ),
                  onPressed: (){
                    _getNewPics();
                  },
              ),
    

    The problem is, that when I start the app nothing is displayed, so I have to ctrl+S (reload the app) and then I see images, the same i have to do when I click on "Load more" button.

    It would be great, if somebody would help me:)

    • Mohammad Assad Arshad
      Mohammad Assad Arshad almost 4 years
      You prob need too put your get New images method in initstate