so I'm using flutter 2.2 and I'm tring to get data from an sqlite database but it's always coming as null
data_helper
class DataBaseHelper{
static final _dataBaseName = 'test.db';
static final _dataBaseVerison = 1;
static final _tabel = 'person';
DataBaseHelper._privateConstructor();
static final DataBaseHelper instance = DataBaseHelper._privateConstructor();
static Database? _database;
Future<Database> get database async {
if (_database != null) return _database!;
_database = await initDatabase();
return _database!;
}
@override
Future<Database> initDatabase() async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, _dataBaseName);
var exists = await databaseExists(path);
if (!exists) {
try {
await Directory(dirname(path)).create(recursive: true);
} catch (_) {}
ByteData data = await rootBundle.load(join("assets", _dataBaseName));
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(path).writeAsBytes(bytes, flush: true);
}
Database newDatabase = await openDatabase(path, version: _dataBaseVerison);
return newDatabase;
}
getAllRows(String tabel) async {
Database db = await instance.database;
var result = await db.query(tabel) ;
print("All Data : $result");
return result.toList();
}
}
person_model
class Person {
Person({
int? id,
String? firstname,
String? lastName,}){
_id = id;
_firstname = firstname;
_lastName = lastName;
}
Person.fromJson(dynamic json) {
_id = json['id'];
_firstname = json['Firstname'];
_lastName = json['lastName'];
}
int? _id;
String? _firstname;
String? _lastName;
int? get id => _id;
String? get firstname => _firstname;
String? get lastName => _lastName;
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
map['id'] = _id;
map['Firstname'] = _firstname;
map['lastName'] = _lastName;
return map;
}
}
main
class _MyHomePageState extends State<MyHomePage> {
DataBaseHelper dbHelper = DataBaseHelper.instance;
List<Person> person= List.empty(growable: true);
@override
void initState() {
super.initState();
dbHelper.getAllRows('person').then((rows){
setState(() {
rows.forEach((row) {
person.add(Person.fromJson(row));
});
});
});
}
@override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(title: Text('SQLite Example')),
body: Center(
child: ListView.builder(
itemCount:person.length,
itemBuilder: (context,index){
return ListTile(
title:Text('${person[index].name}}.'),
);
}),
),
);
}
}
data from database when I print it All Data : [{id:1,Firstname:test,lastName:test}]
so i know that when i send data to fromjason it goes as null not a string and I tried JSON.decode(row) didn't work as type 'QueryRow' is not a subtype of type 'String'
CodePudding user response:
Need to pass row
data in fromJson and follow the below code
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<Person> person = List.empty(growable: true);
List<Map> data = [
{"id": 1, "Firstname": "test", "lastName": "test"}
];
@override
void initState() {
super.initState();
data.forEach((row) {
person.add(Person.fromJson(row));
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: Text('SQLite Example')),
body: Center(
child: ListView.builder(
itemCount: person.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${person[index].firstname} ${person[index].lastName}'),
);
}),
),
),
);
}
}
class Person {
Person({
int? id,
String? firstname,
String? lastName,
}) {
_id = id;
_firstname = firstname;
_lastName = lastName;
}
Person.fromJson(dynamic json) {
_id = json['id'];
_firstname = json['Firstname'];
_lastName = json['lastName'];
}
int? _id;
String? _firstname;
String? _lastName;
int? get id => _id;
String? get firstname => _firstname;
String? get lastName => _lastName;
Map<String, dynamic> toJson() {
final map = <String, dynamic>{};
map['id'] = _id;
map['Firstname'] = _firstname;
map['lastName'] = _lastName;
return map;
}
}