Flutter 颤振:如何显示列表中的项目数

Flutter 颤振:如何显示列表中的项目数,flutter,Flutter,在学习颤振时,我编写了一个CRUD程序,其中显示了项目列表。我想在屏幕底部显示列表中的项目数,但我一直无法实现。当前显示的代码(在末尾)包含一个BottomNavigationBar和一个BottomNavigationBarItem,我试图在其中显示列表中的项目数,即: title: Text("Items = $this.itemCount")), // title: Text("")), 但是,它只显示了项目数量的“…”。如果有人能告诉我如何达到我的要求,

在学习颤振时,我编写了一个CRUD程序,其中显示了项目列表。我想在屏幕底部显示列表中的项目数,但我一直无法实现。当前显示的代码(在末尾)包含一个BottomNavigationBar和一个BottomNavigationBarItem,我试图在其中显示列表中的项目数,即:

                title: Text("Items = $this.itemCount")), // title: Text("")),
但是,它只显示了项目数量的“…”。如果有人能告诉我如何达到我的要求,我将不胜感激


class NotesList extends StatefulWidget {
  @override
  NotesListPageState createState() => NotesListPageState();
}

class NotesListPageState extends State<NotesList> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('Notes List'),
          centerTitle: true,
        ),
        body: new Container(
          padding: new EdgeInsets.all(16.0),
          child: new FutureBuilder<List<Map>>(
            future: fetchDataFromDb(),
            builder: (context, snapshot) {
              if (snapshot == null) {
                return Container(
                  alignment: AlignmentDirectional.center,
                  child: CircularProgressIndicator(),
                );
              } else if (snapshot.hasData) {
                return ListView.builder(
                    itemCount: snapshot == null ? 0 : snapshot.data.length,
                    itemBuilder: (context, index) {
                      return Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            ListTile(
                                leading: (IconButton /* Edit */ (
                                    color: Colors.blue,
                                    icon: new Icon(Icons.edit),
                                    onPressed: () => _showEditScreen(
                                        Crud.eUpdate, snapshot.data[index]))),
                                onLongPress: () => _showEditScreen(
                                    Crud.eRead, snapshot.data[index]),
                                trailing: (IconButton(
                                    color: Colors.red,
                                    icon: new Icon(Icons.delete),
                                    onPressed: () => _showEditScreen(
                                        Crud.eDelete, snapshot.data[index])))),
                          ]);
                    });
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");
              } else {
                return new Text("No data in table");
              }
            },
          ),
        ),
        bottomNavigationBar: BottomNavigationBar(
          onTap: (int index) {
            if (index == 1) {
              Navigator.of(context).pop();
            }
          },
          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.info),
                title: Text("Items = $this.itemCount")), // title: Text("")),
            BottomNavigationBarItem(
              icon: Icon(Icons.add),
              title: Text('Create'),
            ),
          ],
        ));
  }
'''

类NotesList扩展StatefulWidget{
@凌驾
NotesListPageState createState()=>NotesListPageState();
}
类NotesListPageState扩展状态{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“注释列表”),
标题:对,
),
主体:新容器(
填充:新边缘设置。全部(16.0),
孩子:新未来建设者(
future:fetchDataFromDb(),
生成器:(上下文,快照){
如果(快照==null){
返回容器(
对齐:对齐方向.center,
子对象:CircularProgressIndicator(),
);
}else if(snapshot.hasData){
返回ListView.builder(
itemCount:snapshot==null?0:snapshot.data.length,
itemBuilder:(上下文,索引){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
列表砖(
前导:(图标按钮/*编辑*/(
颜色:颜色,蓝色,
图标:新图标(Icons.edit),
按下时:()=>\u显示编辑屏幕(
Crud.eUpdate,snapshot.data[索引]),
onLongPress:()=>\u显示编辑屏幕(
Crud.eRead、snapshot.data[索引],
拖尾:(图标按钮)(
颜色:颜色,红色,
图标:新图标(图标。删除),
按下时:()=>\u显示编辑屏幕(
Crud.eDelete、snapshot.data[索引]),
]);
});
}else if(snapshot.hasrerror){
返回文本(“${snapshot.error}”);
}否则{
返回新文本(“表中无数据”);
}
},
),
),
底部导航栏:底部导航栏(
onTap:(int索引){
如果(索引==1){
Navigator.of(context.pop();
}
},
项目:[
底部导航气压计(
图标:图标(Icons.info),
标题:Text(“Items=$this.itemCount”),//标题:Text(“”),
底部导航气压计(
图标:图标(Icons.add),
标题:文本(“创建”),
),
],
));
}
'''

像这样显示文本-

title: Text("Items = ${this.itemCount}")),
或类似—

title: Text("Items = " + this.itemCount)),

像这样显示你的文本-

title: Text("Items = ${this.itemCount}")),
或类似—

title: Text("Items = " + this.itemCount)),

在state类中,在顶部添加

int count = 0;
添加代码

    WidgetsBinding.instance
    .addPostFrameCallback((_) {
   setState(){
count = snapshot.data.length;
}
});
在这两条线之间

else if (snapshot.hasData) {
return ListView.builder(
并将title属性更改为

title: Text("Items = $count")),

在state类中,在顶部添加

int count = 0;
添加代码

    WidgetsBinding.instance
    .addPostFrameCallback((_) {
   setState(){
count = snapshot.data.length;
}
});
在这两条线之间

else if (snapshot.hasData) {
return ListView.builder(
并将title属性更改为

title: Text("Items = $count")),

嗯。我的错,我错过了,但它不起作用。“getter'itemCount'没有为类'NotesListPageState'定义。这只是我尝试过的许多事情之一。嗯。我的错,我错过了,但它不起作用。”getter“itemCount”不是为类“NotesListPageState”定义的。这只是我尝试过的许多事情之一。魔法,谢谢。我花了几个小时试图解决它。@BrianOh很高兴能帮上忙Magic,谢谢。我花了几个小时试图解决它。@BrianOh很高兴能帮上忙