Flutter 未来的建设者不是建筑

Flutter 未来的建设者不是建筑,flutter,sharedpreferences,flutter-futurebuilder,Flutter,Sharedpreferences,Flutter Futurebuilder,我试图包括FutureBuilder,但它进入循环进程指示器(),在通过从SharedReferences调用填充“time”值并且ConnectionState完成后,不会加载实际的屏幕代码。它只是卡在CircularProgressIndicator()中 我错过了什么 Future<int> getTime() async { await MySharedPreferences.instance.getIntValue("time_key").then(

我试图包括
FutureBuilder
,但它进入
循环进程指示器()
,在通过从SharedReferences调用填充“time”值并且
ConnectionState
完成后,不会加载实际的屏幕代码。它只是卡在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),
         .....
Future getTime()异步{
等待MySharedPreferences.instance.getIntValue(“时间键”)。然后((值)=>
设置状态(){
时间=价值;
}));
返回时间;
@凌驾
void initState(){
super.initState();
MySharedPreferences.instance
.getStringValue(“标题和键”)
.然后((值)=>设置状态(){
标题=价值;
}));
控制器=
AnimationController(vsync:this,
持续时间:持续时间(
秒:时间);
控制器2=
AnimationController(vsync:this,
持续时间:持续时间(
秒:时间);
控制器3=
AnimationController(vsync:this,
持续时间:持续时间(
秒:1);
....}
@凌驾
小部件构建(构建上下文){
返回式示波器(
onWillPop:()async=>false,
孩子:脚手架(
背景颜色:Colors.black,
正文:未来建设者
(
future:getTime(),
生成器:(BuildContext上下文,异步快照){
打印(快照);
打印(时间);
if(snapshot.connectionState==connectionState.done){
打印(时间);
返回安全区(
最小值:所有常数边缘集(20.0),
子:堆栈(
儿童:[
容器(
儿童:
对齐(
对齐:分馏Loffset.topCenter,
孩子:AspectRatio(
aspectRatio:1.0,
子:容器(
高度:MediaQuery
.of(上下文)
.尺寸
.身高/2,
宽度:MediaQuery
.of(上下文)
.尺寸
.身高/2,
装饰:盒子装饰(
//形状:BoxShape.rectangle,
颜色:颜色,黑色,
图像:装饰图像(
图片:资产评估(
“assets/images/moon.png”),
fit:BoxFit.fill,
)
),
),
),
),
),
生成动画(),
],
),
);
}
否则{
返回循环ProgressIndicator();
}
}
),
),
);
}
构建动画(){
返回动画生成器(
动画:控制器,
生成器:(上下文,子对象){
返回堆栈(
儿童:[
填充物(
填充:所有边缘设置(0.0),
子:列(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
扩大(
子对象:对齐(
对齐:分馏Loffset.bottomCenter,
孩子:AspectRatio(
aspectRatio:1.0,
子:堆栈(
儿童:[
填充物(
填充:EdgeInsets.only(顶部:MediaQuery.of(context.size.height/6),
子:列(
儿童:[
正文(
title.toString(),
样式:TextStyle(
字体大小:20.0,
颜色:颜色。黑色,fontWeight:fontWeight。粗体,),
),
新容器(
孩子:新中心(
孩子:新的倒计时(
动画:新StepTween(
开始:时间,
完:0,,
).设置(控制器)动画,
.....

对于初学者,您不需要为未来与FutureBuilder一起使用的结果设置状态。FutureBuilder类的全部要点就是为您处理这些。此外,最好不要混合使用
。然后()
等待
直到你有更多的经验。它们一起工作得很好,但在你还在学习的时候,一次只专注于一个概念

这是您在精简后的方法(如果这仍然值得一个方法,或者如果您希望将该代码直接放入
InState
中,您可以选择):

然后,您可以在
FutureBuilder
中使用它:

body: FutureBuilder<int>(
        future: futureIntFromPreferences,
        builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
body:FutureBuilder(
未来:未来,
生成器:(BuildContext上下文,异步快照){

有关详细说明,请阅读我看到的代码
FutureBuilder
builds,甚至多次添加
print
Future<int> futureIntFromPreferences;

@override
void initState() {
  super.initState();

  futureIntFromPreferences = getTime();
}
body: FutureBuilder<int>(
        future: futureIntFromPreferences,
        builder: (BuildContext context, AsyncSnapshot<int> snapshot) {