我试过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}"),);
}
}
}
)
);
}
现在让我们假设未来是一个HTTP调用401错误结束,指示用户未经批准。此时,我希望应用程序清除存储并重定向到登录页面的任何令牌或重新构建应用程序。但我不能调用一个方法,在构建函数中这样做,我不认为didUpdateWidget()
保证被调用,因为在调用build
之前未来可能会返回它的值?也许我正在接近这个完全错误的,但有没有办法在Flutter中做到这一点?
我已经调整了我的代码一点点。 – aziza