Flutter - Fetch records from database and display in ListView Builder

前端 未结 2 1819
北荒
北荒 2021-01-15 21:55

I\'m working on a Flutter project and using Sqflite database. I\'ve managed to save data in db. Now I am trying to get list of all records from database based on table name

2条回答
  •  野的像风
    2021-01-15 22:33

    You could use a FutureBuilder to get and display your data :

            class _EmployeesListScreenState extends State {
              var db = new DatabaseHelper(); // CALLS FUTURE
    
              @override
              Widget build(BuildContext context) {
                return Scaffold(
                  appBar: AppBar(
                    title: Text('List Of Employees'),
                  ),
                  body: FutureBuilder(
                    future: db.getAllRecords("tabEmployee"),
                    initialData: List(),
                    builder: (context, snapshot) {
                      return snapshot.hasData
                          ? ListView.builder(
                              itemCount: snapshot.data.length,
                              itemBuilder: (_, int position) {
                                final item = snapshot.data[position];
                                //get your item data here ...
                                return Card(
                                  child: ListTile(
                                    title: Text(
                                        "Employee Name: " + snapshot.data[position].row[1]),
                                  ),
                                );
                              },
                            )
                          : Center(
                              child: CircularProgressIndicator(),
                            );
                    },
                  ),
                );
              }
            }
    

提交回复
热议问题