SQLite database in flutter
''' {
Future<List<Dataid>> getPerson() async {
var dbC = await db;
List<Map<String, dynamic>> maps =await dbC.rawQuery("SELECT * FROM $TABLE");
List<Dataid> persons = [];
if (maps.length > 0) {
for (int i = 0; i < maps.length; i++) {
persons.add(Dataid.fromMap(maps[i])); ///error is in this line
print(maps.length);
print("${maps[i]}");
print(persons);
} }
return persons; }
''' when I get data from database and add into map it add null value ?? here ''' {
class Dataid {
String id;
String name;
String srname;
Dataid(this.id, this.name, this.srname);
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
'id': id,
'name': name,
'srname': srname,
};
return map;
}
Dataid.fromMap(
Map<String, dynamic> map,
) {
id = map['id'];
name = map['name'];
srname = map['srname'];
}
}
''' my error is: ''' {
print(maps.length);
print("${maps[i]}");
print(persons);
//when i print this result is
means problem
I/flutter (31391): 30
I/flutter (31391): {ID: 1, NAME: david, SRNAME: john}
I/flutter (31391): [Dataid{id: null, name: null, age: null}]
''' problem during add into map a
Thankyou in advance
I'm not sure as I haven't used SQLite
with Flutter
yet, but maybe the problem is that the keys in maps[i]
are uppercase and you are using their lowercase forms in Dataid.fromMap()
.
Try this:
Dataid.fromMap(
Map<String, dynamic> map,
) {
id = map['ID'];
name = map['NAME'];
srname = map['SRNAME'];
}