我试图包含FutureBuilder
,但它进入CircularProgressIndicator()
,并且在通过从SharedPreferences调用填充'time'值并且ConnectionState
是done
之后,不会加载实际的屏幕代码。它只是卡在CircularProgressIndicator((中。
我在这里错过了什么?
Future<int> getTime() async {
await MySharedPreferences.instance.getIntValue("time_key").then((value) =>
setState(() {
time= value;
}));
return time;
@override
void initState() {
super.initState();
MySharedPreferences.instance
.getStringValue("title_key")
.then((value) => setState(() {
title = value;
}));
controller =
AnimationController(vsync: this,
duration: Duration(
seconds: time));
controller2 =
AnimationController(vsync: this,
duration: Duration(
seconds: time));
controller3 =
AnimationController(vsync: this,
duration: Duration(
seconds: 1));
....}
@override
Widget build(BuildContext context){
return WillPopScope(
onWillPop: () async => false,
child: Scaffold(
backgroundColor: Colors.black,
body: FutureBuilder<int>
(
future: getTime(),
builder: ( BuildContext context, AsyncSnapshot<int> snapshot) {
print(snapshot);
print(time);
if (snapshot.connectionState == ConnectionState.done) {
print(time);
return SafeArea(
minimum: const EdgeInsets.all(20.0),
child: Stack(
children: <Widget>[
Container(
child:
Align(
alignment: FractionalOffset.topCenter,
child: AspectRatio(
aspectRatio: 1.0,
child: Container(
height: MediaQuery
.of(context)
.size
.height / 2,
width: MediaQuery
.of(context)
.size
.height / 2,
decoration: BoxDecoration(
//shape: BoxShape.rectangle,
color: Colors.black,
image: DecorationImage(
image: AssetImage(
"assets/images/moon.png"),
fit: BoxFit.fill,
)
),
),
),
),
),
build_animation(),
],
),
);
}
else {
return CircularProgressIndicator();
}
}
),
),
);
}
build_animation() {
return AnimatedBuilder(
animation: controller,
builder: (context, child) {
return Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.all(0.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: AspectRatio(
aspectRatio: 1.0,
child: Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top:MediaQuery.of(context).size.height / 6),
child: Column(
children: <Widget>[
Text(
title.toString(),
style: TextStyle(
fontSize: 20.0,
color: Colors.black,fontWeight: FontWeight.bold,),
),
new Container(
child: new Center(
child: new Countdown(
animation: new StepTween(
begin: time,
end: 0,
).animate(controller),
.....
对于初学者,您不需要为FutureBuilder使用的Future的结果setState
。FutureBuilder类的全部目的就是为您处理这些问题。此外,最好不要混合.then()
和await
,直到你有更多的经验。它们配合得很好,但在你还在学习的时候,一次只专注于一个概念。
这是修剪后的方法(如果这仍然值得一个方法,或者如果你想直接将代码放入iniState
,你可以选择(:
Future<int> getTime() async {
final value = await MySharedPreferences.instance.getIntValue("time_key");
return value;
}
您应该而不是将该方法提供给FutureBuilder,否则您将在每次出于任何原因调用build
时重新启动它。
所以你的initState
应该是这样的:
Future<int> futureIntFromPreferences;
@override
void initState() {
super.initState();
futureIntFromPreferences = getTime();
}
然后你可以在你的FutureBuilder
:中使用它
body: FutureBuilder<int>(
future: futureIntFromPreferences,
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
有关详细解释,请阅读"什么是未来"以及我如何使用它?