簡體   English   中英

Flutter/Dart - 在 Future 中使用 ChangeNotifier 或 setState 之類的東西?

[英]Flutter/Dart - Use something like ChangeNotifier or setState within Future?

我將SocialProviderChangeNotifier一起使用。 我希望它在用戶登錄並發布到 web 上的數據庫后更新其Consumer偵聽器小部件。

目前,我正在調用Future方法,該方法在成功發布 http 后將新值插入共享首選項文件,但這不會更新 UI,直到調用加載共享首選項的頁面。

為了查看即時更新,有什么方法可以從Future中訪問ChangeNotifier或其他類型的setState類型 function ? 這是未來;

Future<void> postSocialData(String email) async {
    final url = "http://example.com/example.php?currentemail=$email"          

  final response = await http.get(url);
  String currentuserid;

  if (response.statusCode == 200) {

    currentuserid =  response.body;

      setSharedPreferences(
      currentavatar: "http://example.com/" + response.body + "-user.jpg",
      currentemail: email,
      currentlogged: true,
      currentuserid: currentuserid,
    );

    print(response.body);

  } else {

    throw Exception('We were not able to successfully post social data.');
  }
}

關於如何從Future方法獲得即時更新的任何想法?

事實證明,我能夠將Future<void> postSocialData插入 class SocialProvider 的 scope 中, class SocialProvider with ChangeNotifier本身,因此在 Future<void> postSocialData 中使用ChangeNotifier來提醒Consumers/listeners

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM