Monday, April 8, 2024

SQFlite List Json With Api ( Update function for insertSqlite ) in flutter

SQFlite List Json With Api (Update function ) in flutter

which data is not inserted then update method call

{

"result" : [

{

"id" : 1,

"name" : "Avi",

"age" : 22

},

{

"id" : 2,

"name" : "Avi",

"age" : 22

}

],

"status" : 'ok'

}


1....

String sitename="gist.githubusercontent.com";
String path="/Avinashvns/749bdbee6d1d5b4f7e53d3544b63f50c/raw/805b764d3ce34d87375f35cdac5f4905e525df3b/sqlitebook.json";
Map<String,String> map=Map();
final res= await ApiCaller.getApi(sitename, path, map);
print("Res ${res['result']}");

List<dynamic> results = res['result'] as List<dynamic>;
BookModel bookModel = BookModel.fromJson({
"result": results,
"status": "ok",
});

// Iterate over the result list and insert each book into the database
for (Result result in bookModel.result) {

// await DbHandler().insertBook(result);
List<Result> existingRecords = await DbHandler().readBook();
bool recordExists = existingRecords.any((element) => element.id == result.id);

if (recordExists) {
// Update the existing record
await DbHandler().updateBook(result);
print("Record with ID ${result.id} updated");
} else {
// Insert the new record
await DbHandler().insertBook(result);
print("Record with ID ${result.id} inserted");
}
}

// await DbHandler().insertBook(
// Result(
// // id: 2,
// book: nameController.text,
// price: int.parse(ageController.text)
// )
// );
print("Inserted");
print(nameController.text + ageController.text);

              },), 


2. Same As 


No comments:

Post a Comment

Featured post

Compress Image With Show File Size & Resolution in Flutter

 Compress Image With Show File Size & Resolution  1.Multiple File Image Compress with file Size import 'dart:io' ; import 'p...

LightBlog