How can I display an image or text when my List is empty?
7,039
Solution 1
Check length or array and decide which widget to return.
Widget getTasListView() {
return arrList.isNotEmpty
? ListView.builder(
itemCount: arrList.length,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.white,
elevation: 2.0,
child: Column(
children: <Widget>[
ListTile(
title: Text(this.list[position].task),
onLongPress: () =>
navigateToDetail(this.list[position], 'Edit Task'),
)
],
),
);
})
: Center(child: Image.asset("path/image/img.png"));
}
And use arrList.length
instead of itemCount: count
.
Solution 2
Overview: I map a list of SourceClass into a MyCard class list called cards. MyCard runs a Card widget with the SourceClass fields used in it. I check the cards length to determine if an "No records found image or message should be displayed"
@override
Widget build(BuildContext context) {
List<Widget> cards = mylist
?.map((mylist) =>
MyCard(list: listSourceClass))
?.toList();
return new Scaffold(
key: this._scaffoldKey,
appBar: setAppBar(
this._scaffoldKey,
context,
"abc"),
body: cards.length == 0
? Text("No Records Found",style:NoRecordsFoundStyle)
: ListView.builder(
itemCount: cards?.length,
itemBuilder: (BuildContext context, int index) {
return cards[index];
}),
Author by
Admin
Updated on December 16, 2022Comments
-
Admin over 1 year
I am new in flutter I want to display an Image or Text like
No Record Found
when I deleted all the items from listView or listView is empty. In Android I usedsetEmptyView()
for this condition but I have no idea how can I do this in flutter. Currently I am passing ListCan anyone help me please ?DatabaseHelper databaseHelper = DatabaseHelper(); List<TaskModel> list; int count = 0; TaskModel taskModel; @override Widget build(BuildContext context) { if (list == null) { list = List<TaskModel>(); updateListView(); } return Scaffold( appBar: AppBar( title: Text('Task'), ), floatingActionButton: FloatingActionButton( backgroundColor: Colors.purple, child: Icon(Icons.add), onPressed: () { Navigator.push(context, new MaterialPageRoute<void>(builder: (context) { return new CreateTask(TaskModel('', '', '', '', '', '', ''),'Add Task'); })); }), body: getTasListView() ); } ListView getTasListView(){ return ListView.builder( itemCount: count, itemBuilder: (BuildContext context, int position){ return Card( color: Colors.white, elevation: 2.0, child: Column( children: <Widget>[ ListTile( title: Text(this.list[position].task), onLongPress: () => navigateToDetail(this.list[position],'Edit Task'), ) ], ), ); } ); } }