NULL value before incrementing

Asked

Viewed 52 times

1

I am passing values from one screen to another, by clicking a button on the home screen send the data to my profile. But if I do not click this button and go to my profile screen the fields referring to the values are returning me NULL. I wish they’d start with Zero but I’m not getting it.

Screen profile.Dart

class ProfilePage extends StatefulWidget {
   final int valueScore;
   final int valueLifes;

   ProfilePage({Key key, this.valueScore, final int valueLifes;})
  : super(key: key);
 }

Further down in the same profile.Dart file I call these values to be shown on screen.

Text('${widget.valueScore}'),
Text('${widget.valueLifes}'),

In the.Dart home I have the following setting

class _HomePageState extends State<HomePage> {
     int score;
     int saveLifes;

 void increment(){
   setState(() {
      score+=10;
      saveLifes+= 3;
  });
}

Still in the same.Dart home file, I have the button that calls the function to increment the value and pass the data to the profile screen

increment();
Navigator.of(context).push(
  MaterialPageRoute (builder: (BuildContext context) =>
      ProfilePage(valueScore: score, valueLifes: saveLifes,)),
);

If you can help in another detail, whenever I click on the button it passes the data to the profile screen and calls it without the user having clicked, I would like you to just pass the data and then when the user wanted to go there check.

1 answer

3


To start variables with zeroed value do so:

class ProfilePage extends StatefulWidget {
   ProfilePage({Key key, this.valueScore=0, this.valueLifes=0}): super(key: key);

   final int valueScore;
   final int valueLifes;
 }

This way if the parameter is not informed it will come with 0.

For your other problem, is the following, whenever you call the method Navigator.of(context).push() he will call up the screen and display it. To do what you want to create two buttons, in one you increment the variables and in the other you call the screen by passing these incremented variables.

Following example:

class _HomePageState extends State<HomePage> {
     int score;
     int saveLifes;

 void increment(){
   setState(() {
      score+=10;
      saveLifes+= 3;
  });


  @override
  Widget build(BuildContext context){
    return Scaffold(
      body: Column(
        children: <Widget>[
          RaisedButton(
            child: Text("Incrementar"),
            onPressed: (){
                increment();
            }
          ),
          RaisedButton(
            child: Text("Ir pro perfil"),
            onPressed: (){
              Navigator.of(context).push(MaterialPageRoute (builder: (BuildContext context) => 
                ProfilePage(valueScore: score, valueLifes: saveLifes)));
            }
          )
        ]
      )
  }
}

Explanation

In your case, you will be incrementing the variables on the Home Page and there they stayed, then the moment you open the Profile Page you will send the data to her, and not every time you increase the values on the Home Page. It would not make sense to modify the data in Profile Page and it is not being viewed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.