我想解决以下情况:
正在显示(推送)屏幕/小部件。这个屏幕需要:
——————————————————————————
我得出的结论是,如果没有:
值得一提的是,颤振对我来说是新的,我可能会遗漏关于框架的重要信息。
——————————————————————————
以上是上述结论的推理。
错误对话限制:
// ... // flutter:在构建期间调用的setState()或markNeedsBuild()。 // flutter:这个Overlay小部件无法标记为需要构建,因为框架已经在// setState()或markNeedstoBuild()// ...
州指标限制:
——————————————————————————
其他片段可以说明这些情况:
// using FutureBuilder
Widget build(BuildContext context) {
return FutureBuilder(
future: someFuture,
builder: (BuildContext context, AsyncSnapshot snapshot) {
// Here we need to display loading while task is not finished and a dialog if it fails
// dialog needs to be performed with a delay
// triggering again the build will need to have a setState called to “reload”
});
}
// using StreamBuilder
Widget build(BuildContext context) {
return StreamBuilder<void>(
stream: status,
builder: (BuildContext context, AsyncSnapshot<void> status) {
// status can represent states (loading/finished/… )
// dialog needs to be displayed with a delay in case the error the current state
});
}
——————————————————————————
达成最终妥协:
——————————————————————————
我无法真正尝试这一点,但这不能解决你的问题吗?
使用setState有什么问题?
Future _calculation;
initState(){
super.initState();
_calculation = fetchData();
}
FutureBuilder<String>(
future: _calculation, // a previously-obtained Future<String> or null
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Press button to start.');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshot.hasError)
//show your error dialog
// show result
return Text('Result: ${snapshot.data}');
}
return null; // unreachable
},
);
new RaisedButton(
child: const Text('Reload'),
color: Theme.of(context).accentColor,
elevation: 4.0,
splashColor: Colors.blueGrey,
onPressed: () {
setState((
// this will trigger the FutureBuilder and update/show the result
_calculation = fetchData();
));
},
),