我想解决以下情况:
一个屏幕/小部件正在显示(按下)。 该屏幕需要:
——————————————————————————
我得出的结论是,如果没有以下内容,您将无法拥有这种行为:
要提及的是,flutter对我来说是新事物,我可能会缺少有关该框架的重要信息。
——————————————————————————
这是上述结论的原因。
错误对话框限制:
// ... // flutter:在构建期间调用setState()或markNeedsBuild()。 // flutter:此Overlay小部件不能标记为需要构建,因为该框架已经在 // setState()或markNeedstoBuild() //...
如果在生成方法之前执行任务,但失败,您将再次遇到相同的错误 // ... // flutter:在之前调用了InheritedFromWidgetOfExactType(_LocalizationsScope)或inheritFromElement() //颤振:_VacationsViewState.initState()已完成。 //...
该构建方法无法触发任务,因为该方法被调用了很多次。
状态指示器限制:
——————————————————————————
其他片段来举例说明这些情况:
// 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
});
}
——————————————————————————
达成最终妥协:
——————————————————————————
答案 0 :(得分:0)
我实际上无法尝试,但是这种解决方法不能解决您的问题吗?
使用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();
));
},
),