【问题标题】:Flutter/Dart - Use something like ChangeNotifier or setState within Future?Flutter/Dart - 在 Future 中使用 ChangeNotifier 或 setState 之类的东西?
【发布时间】:2020-11-15 02:07:02
【问题描述】:

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

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

为了查看即时更新,有没有办法从Future 中访问ChangeNotifier 或其他类型的setState 类型函数?这是未来;

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 使用 FutureBuilder 重建你的 UI
  • 未来的构建器在他们登录后构建用户信息。但是在他们登录之前我如何呈现默认用户信息?

标签: flutter dart setstate flutter-change-notifier


【解决方案1】:

原来我能够在class SocialProvider with ChangeNotifier 本身的范围内插入Future&lt;void&gt; postSocialData,因此在未来使用ChangeNotifier 来提醒Consumers/listeners

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2015-12-14
    • 2021-05-11
    • 2020-01-05
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多