Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView显示项目两次_Android_Flutter_Dart_Flutter Layout - Fatal编程技术网

Android ListView显示项目两次

Android ListView显示项目两次,android,flutter,dart,flutter-layout,Android,Flutter,Dart,Flutter Layout,我正在尝试使用以下内容在Flatter中构建listview: 预期的功能是listview一次应显示1项 class SimpleContentScreen extends StatefulWidget { @override _SimpleContentScreenState createState() => _SimpleContentScreenState(); } class _SimpleContentScreenState extends BaseState<

我正在尝试使用以下内容在Flatter中构建listview:

预期的功能是listview一次应显示1项

class SimpleContentScreen extends StatefulWidget {
  @override
  _SimpleContentScreenState createState() => _SimpleContentScreenState();
}

class _SimpleContentScreenState extends BaseState<SimpleContentScreen> {

  List<SimpleContent> simpleContentList;
  List<SimpleContent> displayList = List();
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {

    simpleContentList = getOOFirstContent();
    displayList.add(simpleContentList[_currentIndex]);

    return Scaffold(
      appBar: buildAppBar("Introduction"),
      body: _buildListView(),
      floatingActionButton: _buildFab(),
    );
  }

  FloatingActionButton _buildFab() => FloatingActionButton(
    onPressed: () {
      if( _currentIndex < simpleContentList.length - 1 ) {
        setState(() {
          _currentIndex = _currentIndex + 1;
          displayList.add(simpleContentList[_currentIndex]);
        });
      }
    },
    child: Icon(Icons.navigate_next),
    foregroundColor: Colors.white,
    backgroundColor: Colors.blueGrey,
  );

  ListView _buildListView() => ListView.builder(
      key: Key("_simple_content_list"),
      itemCount: displayList.length,
      itemBuilder: (context, position) {
        return _buildItemView( displayList[position] );
      }
  );

  _buildItemView(SimpleContent displayList) => Container(
      padding: const EdgeInsets.all(12),
      margin: EdgeInsets.fromLTRB(0, 8, 32, 8),
      decoration: BoxDecoration(color: Colors.blueAccent),
      child : new Text(
          displayList.contentString,
          style: buildTextSimpleContent(20))
  );


}
class SimpleContentScreen扩展StatefulWidget{
@凌驾
_SimpleContentScreenState createState()=>_SimpleContentScreenState();
}
类_SimpleContentScreenState扩展了BaseState{
列表simpleContentList;
List displayList=List();
int _currentIndex=0;
@凌驾
小部件构建(构建上下文){
simpleContentList=getOOFirstContent();
add(simpleContentList[_currentIndex]);
返回脚手架(
appBar:buildAppBar(“简介”),
正文:_buildListView(),
floatingActionButton:_buildFab(),
);
}
FloatingActionButton_buildFab()=>FloatingActionButton(
已按下:(){
if(_currentIndexListView.builder(
key:key(“简单内容列表”),
itemCount:displayList.length,
itemBuilder:(上下文、位置){
返回_buildItemView(显示列表[位置]);
}
);
_buildItemView(SimpleContent displayList)=>容器(
填充:常量边集。全部(12),
边距:从LTRB(0,8,32,8)开始的边距集,
装饰:盒子装饰(颜色:颜色。蓝色调),
儿童:新文本(
displayList.contentString,
样式:buildTextSimpleContent(20))
);
}
在按下FAB键时,它将添加两个项目。为什么会这样?我通过清除displayList并将0中的所有项添加到当前索引中解决了这个问题

我试图设置listview的键,但没有解决这个问题


感谢您的帮助或见解。

setState
调用小部件的
build
方法进行构建

这就是正在发生的事情

onPressed
方法在单击FAB时调用

_currentIndex = _currentIndex + 1;
displayList.add(simpleContentList[_currentIndex]);
这将添加一个新项

但随后再次调用
build
方法 因此,在build方法
displayList.add(simpleContentList[_currentIndex])中再次将元素添加到列表中


解决方案1 除去

simpleContentList = getOOFirstContent();
displayList.add(simpleContentList[_currentIndex]);
build
并将其添加到
initState


解决方案2 删除

displayList.add(simpleContentList[_currentIndex]);
设置状态
,以便只添加一次元素



有关有状态小部件生命周期方法的更多详细信息,请参阅

我的回答是否解决了您的问题谢谢@Mangaldeep Pannu,这很有帮助。