我在尝试 Flutter 时遇到了一个我无法解决的问题。我正在考虑的案例有一个FutureBuilder如下所示的小部件:
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Example Page"),
),
body: new FutureBuilder(
future: _exampleFuture,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting: return new Center(child: new CircularProgressIndicator(),);
default:
if(snapshot.hasError) {
return new Center(child: new Text('Error: ${snapshot.error}'),);
}
else {
return new Center(child: new Text("Result: ${snapshot.data}"),);
}
}
}
)
);
}
Run Code Online (Sandbox Code Playgroud)
现在我们假设 future 是一个 http 调用,最终会出现 401 错误,表明用户未经授权。此时,我希望应用程序删除存储的任何令牌并重定向到登录页面或只是重建应用程序。但我无法调用在构建函数中执行此操作的方法,并且我认为不能didUpdateWidget()保证被调用,因为未来可能会在build调用之前返回它的值?也许我的做法完全错误,但是有没有办法在 Flutter 中做到这一点?