Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display an image or text when my List is empty?

I am new in flutter I want to display an Image or Text like No Record Foundwhen I deleted all the items from listView or listView is empty. In Android I used setEmptyView() 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'),
                )
              ],
            ),
          );
        }
    );
  }
}
like image 249
Saraswat Avatar asked Dec 19 '19 06:12

Saraswat


2 Answers

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.

like image 83
Anand Kore Avatar answered Sep 20 '22 02:09

Anand Kore


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];
            }),
like image 42
Golden Lion Avatar answered Sep 23 '22 02:09

Golden Lion