Home > Mobile >  Flutter - values updating but when displayed they display the old value
Flutter - values updating but when displayed they display the old value

Time:06-01

I was trying to get the values from the database and display them in the page I will navigate into, the problem is when I print the value of my variable it does print the right value but when I navigate to the other page it does not diplay the new value but displays the old value.

here is the function:

FirebaseFirestore _firestore = FirebaseFirestore.instance;
  var docSnapshot;

   Future DisplayData() async {

    docSnapshot = await _firestore.collection('Places').doc(place.PlaceID).get();
    Map<String, dynamic> data = docSnapshot.data();
    place.PlaceName = data['Name'];
    print('place name is : '   place.PlaceName);
    SharedPreferences.getInstance().then((pref){
      pref.setString("PlaceName", place.PlaceName);
    });
  }

and here is where I call the function:

InkWell(
              child: Row(
                  children:[
                    SizedBox(
                      width: MediaQuery.of(context).size.width *0.05,
                    ),

                    Container(
                      width: MediaQuery.of(context).size.width *0.9,
                      height: MediaQuery.of(context).size.width *0.40,
                      ),
                      child: Row(
                        children: [
                          Image.asset('images/PlacesPics/store1.jpg',width: MediaQuery.of(context).size.width *0.20, height: MediaQuery.of(context).size.width *0.2,),
                          SizedBox(
                            width: MediaQuery.of(context).size.width *0.1,
                          ),

                          Container(
                            margin: EdgeInsets.only(bottom: MediaQuery.of(context).size.width *0.06),
                            child: Column(
                              children: [
                                Container(
                                  margin: EdgeInsets.only(right: MediaQuery.of(context).size.width *0.14),
                                  child: Text(
                                    "store1",style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold,fontSize: 15),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ]
              ),
              onTap: () {
                DisplayData();
                print('place id is : '   place.PlaceID);
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => Place()));
              }
          ),

it prints the new value on the terminal but when I try to show it inside a text in the page I am navigating into it does not display the new value.

The code where I show it in the other page:

Text(
                place.PlaceName,
              ),

CodePudding user response:

You should call sharedpreference like this on the page where you want to use it's value.

final prefs = await SharedPreferences.getInstance();
final String? place = prefs.getString('PlaceName');

Use it in your widget

Text(
       place,
    ),
  • Related