简体   繁体   English

FutureBuilder 能够获取数据,但使用 Flutter 多次获取数据

[英]FutureBuilder is able to get data but data is taken multiple times with Flutter

I am using a simple Json like this:我正在使用一个简单的 Json ,如下所示:

[
    {
        "question" : "Sky",
        "answer" : "Blue",
        
    },
    {
        "question" : "Sun",
        "answer" : "Yellow",
        
    },
]

and am able to get the data inside text widgets as i wanted, the problem is jsondecoder takes the data multiple times and creates multiple screen, i know it is because i put FutureBuilder inside the build method and when i put the future outside of build like this:并且能够按照我的意愿在文本小部件中获取数据,问题是jsondecoder 多次获取数据并创建多个屏幕,我知道这是因为我将 FutureBuilder 放在构建方法中,而当我将未来放在构建之外这个:

Future getDataArray() async {
    final dynamic resp =
        DefaultAssetBundle.of(context).loadString('load_json/words.json');
    return resp;
  }

  @override
  void initState() {
    myFuture = getDataArray();
    super.initState();
  }

the var mydata = JsonDecoder().convert(snapshot.data.toString()); var mydata = JsonDecoder().convert(snapshot.data.toString()); is still inside build and FutureBuilder and that's causing the problem.仍在 build 和 FutureBuilder 中,这就是问题所在。 I cannot get var mydata = JsonDecoder().convert(snapshot.data.toString());我无法获得var mydata = JsonDecoder().convert(snapshot.data.toString()); outside of build please help me with this.在构建之外请帮助我。

build method is:构建方法是:

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.purple[900],
      appBar: new AppBar(
        title: Text(widget.title),
      ),
      body: new Padding(
          padding: EdgeInsets.fromLTRB(2, 20, 2, 20),
          child: FutureBuilder(
            future: myFuture,
            builder: (context, snapshot) {
              //decode json
              var mydata = JsonDecoder().convert(snapshot.data.toString());
              return new ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                     
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          GestureDetector(
                            child: Card(
                              color: Colors.indigoAccent[400],
                              shape: RoundedRectangleBorder(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(24.0)),
                              ),
                              elevation: 10.0,
                              shadowColor: Colors.black,
                              child: Stack(
                                fit: StackFit.loose,
                                alignment: Alignment.center,
                                children: <Widget>[
                                  new Container(
                                    width: 100.0,
                                    height: 100.0,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(24.0),
                                      color: Colors.white,
                                    ),
                                    child: Center(
                                      child: Text(
                                        mydata[index]['answer'],
                                        style: TextStyle(
                                          fontFamily: 'Arial',
                                          fontSize: 20,
                                          color: Colors.indigoAccent[400],
                                          height: 1,
                                          fontWeight: FontWeight.bold,
                                        ),
                                        textAlign: TextAlign.center,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              print('button1');
                            },
                          ),
                          
                        ],
                      )
                    ],
                  );
                },
                itemCount: mydata == null ? 0 : mydata.length,
              );
            },
          )),
    );
  }

Hello my dear on the level of your future builder you should first test if the data are available before having to convert the json to be done I recommend you to:您好,亲爱的,在您未来的建造者的水平上,您应该首先测试数据是否可用,然后才能完成 json 的转换,我建议您:

FutureBuilder(
    future: myFuture,
    builder: (context, snapshot) {
      if(snapshot.hasData){
        var mydata = JsonDecoder().convert(snapshot.data.toString());
      }else{
        // no data available
     }
 );

Community, I have used a ListView.builder, and itemCount was causing the problem not the json, i have removed the ListView.builder and now the problem is solved.社区,我使用了 ListView.builder,并且 itemCount 导致问题不是 json,我已经删除了 ListView.builder,现在问题已解决。 In case there are others who mistake their errors' cause like I did, this question might stay, but If you think this problem is not that common to face, I might delete it.如果有其他人像我一样误会他们的错误原因,这个问题可能会保留,但如果你认为这个问题并不常见,我可能会删除它。 Please say so in the comments.请在评论中这样说。 Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM