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
This might not be correct code, since I've not tested this, but this is how list view builder works and try using async await. Cleans up code quite a bit!
import 'package:flutter/material.dart';
import 'package:flutter_with_db_single_helper/helpers/database_helper.dart'
class EmployeesListScreen extends StatefulWidget {
@override
_EmployeesListScreenState createState() => _EmployeesListScreenState();
}
class _EmployeesListScreenState extends State {
List _records;
@override
initState(){
super.initState();
}
Future _getRecords() async {
var res = await db.getAllRecords("tabEmployee");
setState((){
_records = res;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List Of Employees'),
),
body: _records==null ? Container():ListView.builder(
itemCount: _records.length,
itemBuilder: (_, int position) {
return Card(
child: ListTile(
title:
Text("Employee Name: ", _records[position]),
),
);
},
),
);
}
}
Or you can use a future builder, as the other answer shows. :)