我正在SocialProvider
使用ChangeNotifier
. 我希望它Consumer
在用户登录并发布到网络上的数据库后更新其侦听器小部件。
目前,我正在调用一种Future
方法,该方法在成功的 http 发布后将新值插入共享首选项文件,但这不会更新 UI,直到调用加载共享首选项的页面。
为了查看即时更新,有什么方法ChangeNotifier
可以setState
从Future
? 这是未来;
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
?