Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
Want to ask again ... try implementing the data API stored in a local database ...
the form of the JSON API The object in which there is a JSON Array ... there was an error when wanting to load data from the API to Local database ... roughly this is an error because of "the wrong model or what
Api Service
class MealApiProvider {
Future<List<Categories>> getAllMeal() async {
var url = "https://www.themealdb.com/api/json/v1/1/categories.php";
Response response = await Dio().get(url);
print("Hasil Respon ${response.data}");
return (response.data as List).map((employee) {
print('Inserting $employee');
DBProvider.db.insertMeals(Categories.fromJson(employee));
}).toList();
Model
class DataMeal {
final List<Categories> categories;
DataMeal({this.categories});
@override
String toString() {
return 'DataMeal{categories: $categories}';
factory DataMeal.fromJson(Map<String, dynamic> json) {
return DataMeal(
categories: List<Categories>.from(
json["categories"].map(
(categories) {
return Categories.fromJson(categories);
Map<String, dynamic> toJson() => {
"categories": List<dynamic>.from(
categories.map(
(x) => x.toJson(),
Local DB
initDB() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
final String path = join(documentsDirectory.path, '$nameDatabase');
print('insert database $path');
return await openDatabase(path, version: 1, onOpen: (db) {},
onCreate: (Database db, int version) async {
await db.execute('CREATE TABLE $nameTable('
'idCategory,'
'strCategory TEXT,'
'strCategoryThumb TEXT,'
'strCategoryDescription TEXT'
')');
insertMeals(DataMeal newMeal) async {
await deleteAllMeal();
final Database db = await database;
final res = await db.insert("$nameTable", newMeal.toJson());
print("inserting data $res");
return res;
Error
return (response.data as List).map((employee)
Respon Data
–
class MealApiProvider {
Future<List<Categories>> getAllMeal() async {
var url = "https://www.themealdb.com/api/json/v1/1/categories.php";
Response response = await Dio().get(url);
print("Hasil Respon ${response.data}");
//***Change below line****
return (response.data['categories'] as List).map((employee) {
print('Inserting $employee');
DBProvider.db.insertMeals(Categories.fromJson(employee));
}).toList();
instead of
class MealApiProvider {
Future<List<Categories>> getAllMeal() async {
var url = "https://www.themealdb.com/api/json/v1/1/categories.php";
Response response = await Dio().get(url);
print("Hasil Respon ${response.data}");
//*** I change the below line !
return (response.data as List).map((employee) {
print('Inserting $employee');
DBProvider.db.insertMeals(Categories.fromJson(employee));
}).toList();
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.