Flutter - 值更新但显示时显示旧值

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

我试图从数据库中获取值并将它们显示在我将导航到的页面中,问题是当我打印我的变量值时它确实打印了正确的值但是当我导航到另一个时页面不显示新值而是显示旧值。

函数如下:

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);
    });
  }

这里是我调用函数的地方:

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()));
              }
          ),

它在终端上打印新值,但是当我尝试在页面的文本中显示它时,我导航到它并没有显示新值。

我在另一页显示的代码:

Text(
                place.PlaceName,
              ),

您应该在要使用它的值的页面上像这样调用 sharedpreference。

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

在您的小部件中使用它

Text(
       place,
    ),