0

我正在SocialProvider使用ChangeNotifier. 我希望它Consumer在用户登录并发布到网络上的数据库后更新其侦听器小部件。

目前,我正在调用一种Future方法,该方法在成功的 http 发布后将新值插入共享首选项文件,但这不会更新 UI,直到调用加载共享首选项的页面。

为了查看即时更新,有什么方法ChangeNotifier可以setStateFuture? 这是未来;

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

4

1 回答 1

0

事实证明,我能够Future<void> postSocialData在自身范围内插入class SocialProvider with ChangeNotifier,因此ChangeNotifier在 Future 中使用 The 来提醒Consumers/listeners.

于 2020-07-27T06:51:15.710 回答