Old value kept after a setState – Flutter

Issue

I have a problem with a simple code.
I have a Futurbuider and in this one in a text that I updated with setState but it returns me each time the text present in my Future

class _HomejeuState extends State<Homejeu> {
  bool _visible = true;
  var list = [];
  var texte1 = '';
  Future<String> showLoginPage() async {
    var sharedPreferences = await SharedPreferences.getInstance();
     
    var valeurlangue = sharedPreferences.getString("Langconfig") ?? 'NON';
    if (valeurlangue == 'FR') {
      list = [1, 2, 3, 4, 5];
      texte1 =
          'bla bla bla bla bla bla';
    } else {
      list = [7, 8, 9, 0];
      texte1 =
          'hello hello  hello hello hello hello';
    }
    return valeurlangue;
  } 

I modify the text via a void function

 void lanceuboule() {
   
    Future.delayed(const Duration(milliseconds: 500), () {
      setState(() {
        
        texteboule = 'text text text text';
      });
    });

    
  }  

I do not understand. I have the impression that completely relaunches the futurbluider widget. how to prevent it if that is the problem.
Thank you

Solution

I found a solution:

I deleted the future widget and modified my future <String> by future <void> then added an initState

Future<void> showLoginPage() async {
    var sharedPreferences = await SharedPreferences.getInstance();
    //sharedPreferences.setString('Langconfig', 'oui');
    var valeurlangue = sharedPreferences.getString("Langconfig") ?? 'NON';
    if (valeurlangue == 'FR') {
      setState(() {
        list = [1, 2, 3, 4, 5];
        texteboule =
           'bla bla bla bla bla bla';
      });
    } else {
      setState(() {
        list = [7, 8, 9, 0];
        texteboule =
            'hello hello  hello hello hello hello';
      });
    }
  }

@override
  void initState() {
    super.initState();
    showLoginPage(); 
  }

Posted on behalf of the question asker

Answered By – Dharman

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *