Flutter: Maintant state of child stateful widget





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a listview.builder inside a stateful widget and i made a separate stateful widget for the item (ImageCard).
inside the ImageCard widget i have a like button when i click it its color change to red(like), gray(dislike).
the problem is that when i scroll down and return back the color is always gray which means that no state is saved!
how can i notify the parent stateful widget to keep the state?



Parent stateful widget



@override
Widget build(BuildContext context) {
return _buildListView(models, _scrollController);
}

Widget _buildListView(
List<PhotoModel> models, ScrollController scrollController) {
return Container(
child: ListView.builder(
controller: scrollController,
itemCount: models.length,
itemBuilder: (context, int index) {
if (index == models.length - 1) {
return SpinKitThreeBounce(
color: Colors.purple,
size: 30.0,
);
} else {
return ImageCard(
models[index].regularPhotoUrl,
models[index].mediumProfilePhotoUrl,
models[index].name,
models[index].color);
}
}));
}


child stateful widget



class ImageCard extends StatefulWidget {
final String imageUrl, userProfilePic, userName, color;

ImageCard(this.imageUrl, this.userProfilePic, this.userName, this.color);

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

class _ImageCardState extends State<ImageCard> {
bool isLiked = false, isFollowing = false;

@override
Widget build(BuildContext context) {
return new Card( ....

void _onLikedBtnClicked() {
setState(() {
if (isLiked)
isLiked = false;
else {
isLiked = true;
}
});
}









share|improve this question























  • So where within ImageCard are you saving the isLiked property? Is it meant to be the color property?

    – SnakeyHips
    Nov 23 '18 at 13:00











  • Its shown in my code that i save it in the state class

    – M.Ali
    Nov 23 '18 at 13:13











  • I se the color of the btn to red if isLiked is true

    – M.Ali
    Nov 23 '18 at 13:14


















0















I have a listview.builder inside a stateful widget and i made a separate stateful widget for the item (ImageCard).
inside the ImageCard widget i have a like button when i click it its color change to red(like), gray(dislike).
the problem is that when i scroll down and return back the color is always gray which means that no state is saved!
how can i notify the parent stateful widget to keep the state?



Parent stateful widget



@override
Widget build(BuildContext context) {
return _buildListView(models, _scrollController);
}

Widget _buildListView(
List<PhotoModel> models, ScrollController scrollController) {
return Container(
child: ListView.builder(
controller: scrollController,
itemCount: models.length,
itemBuilder: (context, int index) {
if (index == models.length - 1) {
return SpinKitThreeBounce(
color: Colors.purple,
size: 30.0,
);
} else {
return ImageCard(
models[index].regularPhotoUrl,
models[index].mediumProfilePhotoUrl,
models[index].name,
models[index].color);
}
}));
}


child stateful widget



class ImageCard extends StatefulWidget {
final String imageUrl, userProfilePic, userName, color;

ImageCard(this.imageUrl, this.userProfilePic, this.userName, this.color);

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

class _ImageCardState extends State<ImageCard> {
bool isLiked = false, isFollowing = false;

@override
Widget build(BuildContext context) {
return new Card( ....

void _onLikedBtnClicked() {
setState(() {
if (isLiked)
isLiked = false;
else {
isLiked = true;
}
});
}









share|improve this question























  • So where within ImageCard are you saving the isLiked property? Is it meant to be the color property?

    – SnakeyHips
    Nov 23 '18 at 13:00











  • Its shown in my code that i save it in the state class

    – M.Ali
    Nov 23 '18 at 13:13











  • I se the color of the btn to red if isLiked is true

    – M.Ali
    Nov 23 '18 at 13:14














0












0








0








I have a listview.builder inside a stateful widget and i made a separate stateful widget for the item (ImageCard).
inside the ImageCard widget i have a like button when i click it its color change to red(like), gray(dislike).
the problem is that when i scroll down and return back the color is always gray which means that no state is saved!
how can i notify the parent stateful widget to keep the state?



Parent stateful widget



@override
Widget build(BuildContext context) {
return _buildListView(models, _scrollController);
}

Widget _buildListView(
List<PhotoModel> models, ScrollController scrollController) {
return Container(
child: ListView.builder(
controller: scrollController,
itemCount: models.length,
itemBuilder: (context, int index) {
if (index == models.length - 1) {
return SpinKitThreeBounce(
color: Colors.purple,
size: 30.0,
);
} else {
return ImageCard(
models[index].regularPhotoUrl,
models[index].mediumProfilePhotoUrl,
models[index].name,
models[index].color);
}
}));
}


child stateful widget



class ImageCard extends StatefulWidget {
final String imageUrl, userProfilePic, userName, color;

ImageCard(this.imageUrl, this.userProfilePic, this.userName, this.color);

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

class _ImageCardState extends State<ImageCard> {
bool isLiked = false, isFollowing = false;

@override
Widget build(BuildContext context) {
return new Card( ....

void _onLikedBtnClicked() {
setState(() {
if (isLiked)
isLiked = false;
else {
isLiked = true;
}
});
}









share|improve this question














I have a listview.builder inside a stateful widget and i made a separate stateful widget for the item (ImageCard).
inside the ImageCard widget i have a like button when i click it its color change to red(like), gray(dislike).
the problem is that when i scroll down and return back the color is always gray which means that no state is saved!
how can i notify the parent stateful widget to keep the state?



Parent stateful widget



@override
Widget build(BuildContext context) {
return _buildListView(models, _scrollController);
}

Widget _buildListView(
List<PhotoModel> models, ScrollController scrollController) {
return Container(
child: ListView.builder(
controller: scrollController,
itemCount: models.length,
itemBuilder: (context, int index) {
if (index == models.length - 1) {
return SpinKitThreeBounce(
color: Colors.purple,
size: 30.0,
);
} else {
return ImageCard(
models[index].regularPhotoUrl,
models[index].mediumProfilePhotoUrl,
models[index].name,
models[index].color);
}
}));
}


child stateful widget



class ImageCard extends StatefulWidget {
final String imageUrl, userProfilePic, userName, color;

ImageCard(this.imageUrl, this.userProfilePic, this.userName, this.color);

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

class _ImageCardState extends State<ImageCard> {
bool isLiked = false, isFollowing = false;

@override
Widget build(BuildContext context) {
return new Card( ....

void _onLikedBtnClicked() {
setState(() {
if (isLiked)
isLiked = false;
else {
isLiked = true;
}
});
}






dart flutter flutter-layout






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 12:51









M.AliM.Ali

564217




564217













  • So where within ImageCard are you saving the isLiked property? Is it meant to be the color property?

    – SnakeyHips
    Nov 23 '18 at 13:00











  • Its shown in my code that i save it in the state class

    – M.Ali
    Nov 23 '18 at 13:13











  • I se the color of the btn to red if isLiked is true

    – M.Ali
    Nov 23 '18 at 13:14



















  • So where within ImageCard are you saving the isLiked property? Is it meant to be the color property?

    – SnakeyHips
    Nov 23 '18 at 13:00











  • Its shown in my code that i save it in the state class

    – M.Ali
    Nov 23 '18 at 13:13











  • I se the color of the btn to red if isLiked is true

    – M.Ali
    Nov 23 '18 at 13:14

















So where within ImageCard are you saving the isLiked property? Is it meant to be the color property?

– SnakeyHips
Nov 23 '18 at 13:00





So where within ImageCard are you saving the isLiked property? Is it meant to be the color property?

– SnakeyHips
Nov 23 '18 at 13:00













Its shown in my code that i save it in the state class

– M.Ali
Nov 23 '18 at 13:13





Its shown in my code that i save it in the state class

– M.Ali
Nov 23 '18 at 13:13













I se the color of the btn to red if isLiked is true

– M.Ali
Nov 23 '18 at 13:14





I se the color of the btn to red if isLiked is true

– M.Ali
Nov 23 '18 at 13:14












3 Answers
3






active

oldest

votes


















2














Flutter will automatically disposes the widget that moves out of screen, and when they re-appear, they will be re-built rather than recovered.



So common practice is to save the state in a high-level widget, which contains at least a complete aspect of business logic and is not going to be disposed anytime soon. Then a change in the state is mapped into child widgets.



For your specific case, a simple solution is: you store the information in the parent widget, and maps them to a ImageCard inside the parent widget's build function.



Add isliked,isfollowing property to the model, then



class SomeParentState extends State<SomeParent> {
List<Model> models;

//.......

@override
Widget build(BuildContext context) {
return _buildListView(models, _scrollController);
}

Widget _buildListView(List<PhotoModel> models,
ScrollController scrollController) {
return Container(
child: ListView.builder(
controller: scrollController,
itemCount: models.length,
itemBuilder: (context, int index) {
if (index == models.length - 1) {
return SpinKitThreeBounce(
color: Colors.purple,
size: 30.0,
);
} else {
return ImageCard(
models[index].regularPhotoUrl,
models[index].mediumProfilePhotoUrl,
models[index].name,
models[index].color,
models[index].isLiked,
models[index].isFollowing,
() {
setState(() {
models[index].isLiked = !models[index].isLiked;
});
},
() {
setState(() {
models[index].isFollowing = !models[index].isFollowing;
});
},
);
}
}));
}
}


class ImageCard extends StatelessWidget{

ImageCard(
//...,
this.isLiked,
this.isFollowing,
this.likeBtnClickedListener,
this.followBtnClickedListener,
)
//...
Widget build(BuildContext context){
return Card(
//.......
IconButton(
onPressed: likeBtnClickedListener,
),
IconButton(
onPressed: followBtnClickedListener,
),
)
}
}


This should basically solve your problem. Anyway, it is easier to access and sync the data in the child widgets in this method.



If you find it easier to just keep the child widget alive, you can read the documentation of AutomaticKeepAliveClientMixin. It will stop flutter from killing this widget when it moves out of sight. But it is risky of causing memory leak.






share|improve this answer


























  • your are right!, i figured out that ListView.builder widget creates and destroys items on demand, and the state is discarded when the item is destroyed.so it is a must to make ImageCard a stateless widget

    – M.Ali
    Nov 23 '18 at 15:02



















1














To maintain the state of a widget inside a ListView, you need to AutomaticKeepAlive or AutomaticKeepAliveMixin (for custom widgets)



This will ensure the State instance is not destroyed when leaving the screen



ListView(
children: [
// Not kept alive
Text('Hello World'),
// kept alive
AutomaticKeepAlive(
child: Text("Hello World"),
),
]
),





share|improve this answer
























  • thank you for this hint, but @First_Strike solution is exactly what i searching for

    – M.Ali
    Nov 23 '18 at 15:06



















-2














You should keep your state separately then. You could make a List<bool> and have one value in there for each of the List items. You probably want to save or use the data at some point anyways, then this mechanism is going to be useless.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447080%2fflutter-maintant-state-of-child-stateful-widget%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Flutter will automatically disposes the widget that moves out of screen, and when they re-appear, they will be re-built rather than recovered.



    So common practice is to save the state in a high-level widget, which contains at least a complete aspect of business logic and is not going to be disposed anytime soon. Then a change in the state is mapped into child widgets.



    For your specific case, a simple solution is: you store the information in the parent widget, and maps them to a ImageCard inside the parent widget's build function.



    Add isliked,isfollowing property to the model, then



    class SomeParentState extends State<SomeParent> {
    List<Model> models;

    //.......

    @override
    Widget build(BuildContext context) {
    return _buildListView(models, _scrollController);
    }

    Widget _buildListView(List<PhotoModel> models,
    ScrollController scrollController) {
    return Container(
    child: ListView.builder(
    controller: scrollController,
    itemCount: models.length,
    itemBuilder: (context, int index) {
    if (index == models.length - 1) {
    return SpinKitThreeBounce(
    color: Colors.purple,
    size: 30.0,
    );
    } else {
    return ImageCard(
    models[index].regularPhotoUrl,
    models[index].mediumProfilePhotoUrl,
    models[index].name,
    models[index].color,
    models[index].isLiked,
    models[index].isFollowing,
    () {
    setState(() {
    models[index].isLiked = !models[index].isLiked;
    });
    },
    () {
    setState(() {
    models[index].isFollowing = !models[index].isFollowing;
    });
    },
    );
    }
    }));
    }
    }


    class ImageCard extends StatelessWidget{

    ImageCard(
    //...,
    this.isLiked,
    this.isFollowing,
    this.likeBtnClickedListener,
    this.followBtnClickedListener,
    )
    //...
    Widget build(BuildContext context){
    return Card(
    //.......
    IconButton(
    onPressed: likeBtnClickedListener,
    ),
    IconButton(
    onPressed: followBtnClickedListener,
    ),
    )
    }
    }


    This should basically solve your problem. Anyway, it is easier to access and sync the data in the child widgets in this method.



    If you find it easier to just keep the child widget alive, you can read the documentation of AutomaticKeepAliveClientMixin. It will stop flutter from killing this widget when it moves out of sight. But it is risky of causing memory leak.






    share|improve this answer


























    • your are right!, i figured out that ListView.builder widget creates and destroys items on demand, and the state is discarded when the item is destroyed.so it is a must to make ImageCard a stateless widget

      – M.Ali
      Nov 23 '18 at 15:02
















    2














    Flutter will automatically disposes the widget that moves out of screen, and when they re-appear, they will be re-built rather than recovered.



    So common practice is to save the state in a high-level widget, which contains at least a complete aspect of business logic and is not going to be disposed anytime soon. Then a change in the state is mapped into child widgets.



    For your specific case, a simple solution is: you store the information in the parent widget, and maps them to a ImageCard inside the parent widget's build function.



    Add isliked,isfollowing property to the model, then



    class SomeParentState extends State<SomeParent> {
    List<Model> models;

    //.......

    @override
    Widget build(BuildContext context) {
    return _buildListView(models, _scrollController);
    }

    Widget _buildListView(List<PhotoModel> models,
    ScrollController scrollController) {
    return Container(
    child: ListView.builder(
    controller: scrollController,
    itemCount: models.length,
    itemBuilder: (context, int index) {
    if (index == models.length - 1) {
    return SpinKitThreeBounce(
    color: Colors.purple,
    size: 30.0,
    );
    } else {
    return ImageCard(
    models[index].regularPhotoUrl,
    models[index].mediumProfilePhotoUrl,
    models[index].name,
    models[index].color,
    models[index].isLiked,
    models[index].isFollowing,
    () {
    setState(() {
    models[index].isLiked = !models[index].isLiked;
    });
    },
    () {
    setState(() {
    models[index].isFollowing = !models[index].isFollowing;
    });
    },
    );
    }
    }));
    }
    }


    class ImageCard extends StatelessWidget{

    ImageCard(
    //...,
    this.isLiked,
    this.isFollowing,
    this.likeBtnClickedListener,
    this.followBtnClickedListener,
    )
    //...
    Widget build(BuildContext context){
    return Card(
    //.......
    IconButton(
    onPressed: likeBtnClickedListener,
    ),
    IconButton(
    onPressed: followBtnClickedListener,
    ),
    )
    }
    }


    This should basically solve your problem. Anyway, it is easier to access and sync the data in the child widgets in this method.



    If you find it easier to just keep the child widget alive, you can read the documentation of AutomaticKeepAliveClientMixin. It will stop flutter from killing this widget when it moves out of sight. But it is risky of causing memory leak.






    share|improve this answer


























    • your are right!, i figured out that ListView.builder widget creates and destroys items on demand, and the state is discarded when the item is destroyed.so it is a must to make ImageCard a stateless widget

      – M.Ali
      Nov 23 '18 at 15:02














    2












    2








    2







    Flutter will automatically disposes the widget that moves out of screen, and when they re-appear, they will be re-built rather than recovered.



    So common practice is to save the state in a high-level widget, which contains at least a complete aspect of business logic and is not going to be disposed anytime soon. Then a change in the state is mapped into child widgets.



    For your specific case, a simple solution is: you store the information in the parent widget, and maps them to a ImageCard inside the parent widget's build function.



    Add isliked,isfollowing property to the model, then



    class SomeParentState extends State<SomeParent> {
    List<Model> models;

    //.......

    @override
    Widget build(BuildContext context) {
    return _buildListView(models, _scrollController);
    }

    Widget _buildListView(List<PhotoModel> models,
    ScrollController scrollController) {
    return Container(
    child: ListView.builder(
    controller: scrollController,
    itemCount: models.length,
    itemBuilder: (context, int index) {
    if (index == models.length - 1) {
    return SpinKitThreeBounce(
    color: Colors.purple,
    size: 30.0,
    );
    } else {
    return ImageCard(
    models[index].regularPhotoUrl,
    models[index].mediumProfilePhotoUrl,
    models[index].name,
    models[index].color,
    models[index].isLiked,
    models[index].isFollowing,
    () {
    setState(() {
    models[index].isLiked = !models[index].isLiked;
    });
    },
    () {
    setState(() {
    models[index].isFollowing = !models[index].isFollowing;
    });
    },
    );
    }
    }));
    }
    }


    class ImageCard extends StatelessWidget{

    ImageCard(
    //...,
    this.isLiked,
    this.isFollowing,
    this.likeBtnClickedListener,
    this.followBtnClickedListener,
    )
    //...
    Widget build(BuildContext context){
    return Card(
    //.......
    IconButton(
    onPressed: likeBtnClickedListener,
    ),
    IconButton(
    onPressed: followBtnClickedListener,
    ),
    )
    }
    }


    This should basically solve your problem. Anyway, it is easier to access and sync the data in the child widgets in this method.



    If you find it easier to just keep the child widget alive, you can read the documentation of AutomaticKeepAliveClientMixin. It will stop flutter from killing this widget when it moves out of sight. But it is risky of causing memory leak.






    share|improve this answer















    Flutter will automatically disposes the widget that moves out of screen, and when they re-appear, they will be re-built rather than recovered.



    So common practice is to save the state in a high-level widget, which contains at least a complete aspect of business logic and is not going to be disposed anytime soon. Then a change in the state is mapped into child widgets.



    For your specific case, a simple solution is: you store the information in the parent widget, and maps them to a ImageCard inside the parent widget's build function.



    Add isliked,isfollowing property to the model, then



    class SomeParentState extends State<SomeParent> {
    List<Model> models;

    //.......

    @override
    Widget build(BuildContext context) {
    return _buildListView(models, _scrollController);
    }

    Widget _buildListView(List<PhotoModel> models,
    ScrollController scrollController) {
    return Container(
    child: ListView.builder(
    controller: scrollController,
    itemCount: models.length,
    itemBuilder: (context, int index) {
    if (index == models.length - 1) {
    return SpinKitThreeBounce(
    color: Colors.purple,
    size: 30.0,
    );
    } else {
    return ImageCard(
    models[index].regularPhotoUrl,
    models[index].mediumProfilePhotoUrl,
    models[index].name,
    models[index].color,
    models[index].isLiked,
    models[index].isFollowing,
    () {
    setState(() {
    models[index].isLiked = !models[index].isLiked;
    });
    },
    () {
    setState(() {
    models[index].isFollowing = !models[index].isFollowing;
    });
    },
    );
    }
    }));
    }
    }


    class ImageCard extends StatelessWidget{

    ImageCard(
    //...,
    this.isLiked,
    this.isFollowing,
    this.likeBtnClickedListener,
    this.followBtnClickedListener,
    )
    //...
    Widget build(BuildContext context){
    return Card(
    //.......
    IconButton(
    onPressed: likeBtnClickedListener,
    ),
    IconButton(
    onPressed: followBtnClickedListener,
    ),
    )
    }
    }


    This should basically solve your problem. Anyway, it is easier to access and sync the data in the child widgets in this method.



    If you find it easier to just keep the child widget alive, you can read the documentation of AutomaticKeepAliveClientMixin. It will stop flutter from killing this widget when it moves out of sight. But it is risky of causing memory leak.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 14:43

























    answered Nov 23 '18 at 14:36









    First_StrikeFirst_Strike

    1649




    1649













    • your are right!, i figured out that ListView.builder widget creates and destroys items on demand, and the state is discarded when the item is destroyed.so it is a must to make ImageCard a stateless widget

      – M.Ali
      Nov 23 '18 at 15:02



















    • your are right!, i figured out that ListView.builder widget creates and destroys items on demand, and the state is discarded when the item is destroyed.so it is a must to make ImageCard a stateless widget

      – M.Ali
      Nov 23 '18 at 15:02

















    your are right!, i figured out that ListView.builder widget creates and destroys items on demand, and the state is discarded when the item is destroyed.so it is a must to make ImageCard a stateless widget

    – M.Ali
    Nov 23 '18 at 15:02





    your are right!, i figured out that ListView.builder widget creates and destroys items on demand, and the state is discarded when the item is destroyed.so it is a must to make ImageCard a stateless widget

    – M.Ali
    Nov 23 '18 at 15:02













    1














    To maintain the state of a widget inside a ListView, you need to AutomaticKeepAlive or AutomaticKeepAliveMixin (for custom widgets)



    This will ensure the State instance is not destroyed when leaving the screen



    ListView(
    children: [
    // Not kept alive
    Text('Hello World'),
    // kept alive
    AutomaticKeepAlive(
    child: Text("Hello World"),
    ),
    ]
    ),





    share|improve this answer
























    • thank you for this hint, but @First_Strike solution is exactly what i searching for

      – M.Ali
      Nov 23 '18 at 15:06
















    1














    To maintain the state of a widget inside a ListView, you need to AutomaticKeepAlive or AutomaticKeepAliveMixin (for custom widgets)



    This will ensure the State instance is not destroyed when leaving the screen



    ListView(
    children: [
    // Not kept alive
    Text('Hello World'),
    // kept alive
    AutomaticKeepAlive(
    child: Text("Hello World"),
    ),
    ]
    ),





    share|improve this answer
























    • thank you for this hint, but @First_Strike solution is exactly what i searching for

      – M.Ali
      Nov 23 '18 at 15:06














    1












    1








    1







    To maintain the state of a widget inside a ListView, you need to AutomaticKeepAlive or AutomaticKeepAliveMixin (for custom widgets)



    This will ensure the State instance is not destroyed when leaving the screen



    ListView(
    children: [
    // Not kept alive
    Text('Hello World'),
    // kept alive
    AutomaticKeepAlive(
    child: Text("Hello World"),
    ),
    ]
    ),





    share|improve this answer













    To maintain the state of a widget inside a ListView, you need to AutomaticKeepAlive or AutomaticKeepAliveMixin (for custom widgets)



    This will ensure the State instance is not destroyed when leaving the screen



    ListView(
    children: [
    // Not kept alive
    Text('Hello World'),
    // kept alive
    AutomaticKeepAlive(
    child: Text("Hello World"),
    ),
    ]
    ),






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 14:37









    Rémi RousseletRémi Rousselet

    37.3k587114




    37.3k587114













    • thank you for this hint, but @First_Strike solution is exactly what i searching for

      – M.Ali
      Nov 23 '18 at 15:06



















    • thank you for this hint, but @First_Strike solution is exactly what i searching for

      – M.Ali
      Nov 23 '18 at 15:06

















    thank you for this hint, but @First_Strike solution is exactly what i searching for

    – M.Ali
    Nov 23 '18 at 15:06





    thank you for this hint, but @First_Strike solution is exactly what i searching for

    – M.Ali
    Nov 23 '18 at 15:06











    -2














    You should keep your state separately then. You could make a List<bool> and have one value in there for each of the List items. You probably want to save or use the data at some point anyways, then this mechanism is going to be useless.






    share|improve this answer




























      -2














      You should keep your state separately then. You could make a List<bool> and have one value in there for each of the List items. You probably want to save or use the data at some point anyways, then this mechanism is going to be useless.






      share|improve this answer


























        -2












        -2








        -2







        You should keep your state separately then. You could make a List<bool> and have one value in there for each of the List items. You probably want to save or use the data at some point anyways, then this mechanism is going to be useless.






        share|improve this answer













        You should keep your state separately then. You could make a List<bool> and have one value in there for each of the List items. You probably want to save or use the data at some point anyways, then this mechanism is going to be useless.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 14:12









        01leo01leo

        551115




        551115






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447080%2fflutter-maintant-state-of-child-stateful-widget%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Paul Cézanne

            UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

            Angular material date-picker (MatDatepicker) auto completes the date on focus out