ListBuilder在同一视图抖动中重复数据的问题

时间:2019-10-09 08:06:13

标签: android mobile flutter flutter-layout

我在listBuilder中生成了一个问题列表。我想为一个列表创建一个视图,因为就目前而言,我有权回答这些问题,但所有视图只能归于一个视图。

但是我想回答我的第一个问题,当我单击下一步按钮时,生成与下一个问题相同的屏幕。

class _Poll extends State<PollPage> {
  List<String> choix = [];


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text("Blackblox"),
        leading: Image.asset(
          'assets/img/logo_ineat.png',
          fit: BoxFit.contain,
          height: 32,
        ),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            new Flexible(
              child: new Container(
                child: new FutureBuilder<List<PollQuestionsDto>>(
                    future: Poll().getPollQuestions(),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      if (snapshot.hasData) {
                        if (snapshot.data != null) {
                          return new Column(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: <Widget>[
                              new Expanded(
                                child: new ListView.builder(
                                  itemCount: snapshot.data.length,
                                  itemBuilder: (BuildContext context, index) {
                                    return PollListViewItem(
                                      entitled: snapshot.data[index].entitled,
                                      answers: snapshot.data[index].answers,
                                    );
                                  },
                                ),
                              ),
                            ],
                          );
                        }
                      } else {
                        new Text("Loading...");
                        return new CircularProgressIndicator();
                      }
                    }),

2 个答案:

答案 0 :(得分:1)

因为您的FutureBuilder一次又一次地开火。您必须避免future参数必须运行一次(例如,像往常一样建议在initState内部)。

每次与屏幕上的任何元素进行交互时,您的State类都会重建,而Poll().getPollQuestions()将再次触发。

执行以下操作:

class _Poll extends State<PollPage> {
Future future;

@override
void initState(){
  future = Poll().getPollQuestions();
  super.initState();
}

并在future内更改FutureBuilder参数:

future: future,

答案 1 :(得分:1)

void initState(){
  super.initState();
  Poll().getPollQuestions();
}