1

Hello Guys I am working on a flutter project Where I am trying to save data in a sqflite database. Since I am new to flutter so I didn't understand where I am doing it wrong or what is the mistake I made.

The error I am getting is

I/flutter (28200): *** WARNING ***
I/flutter (28200): 
I/flutter (28200): Invalid argument {recipeName: Butter Chicken, recipeCategory: Indian Cuisine} with type _InternalLinkedHashMap<String, dynamic>.
I/flutter (28200): Only num, String and Uint8List are supported. See https://github.com/tekartik/sqflite/blob/master/sqflite/doc/supported_types.md for details
I/flutter (28200): 
I/flutter (28200): This will throw an exception in the future. For now it is displayed once per type.
I/flutter (28200): 
I/flutter (28200):     
E/SQLiteLog(28200): (1) near "101": syntax error in "INSERT INTO recipeTable (101, 102, 103, 104, 105, 106, 107, 109, 110) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
E/flutter (28200): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(near "101": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO recipeTable (101, 102, 103, 104, 105, 106, 107, 109, 110) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)) sql 'INSERT INTO recipeTable (101, 102, 103, 104, 105, 106, 107, 109, 110) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' args [{recipeName: Butter Chicken, recipeCategory: India..., {recipeName: Tikka, recipeCategory: Indian Cuisine..., {recipeName: Pizza, recipeCategory: Italian Cuisin..., {recipeName: Burger, recipeCategory: Fast Food}, {recipeName: Karai, recipeCategory: Pakistani Cuis..., {recipeName: Handi, recipeCategory: Indian Cuisine..., {recipeName: Soup, recipeCategory: Starter}, {recipeName: Shawarma, recipeCategory: Eastern Cui..., {recipeName: French Fires, recipeCategory: Fast Fo...]
E/flutter (28200): #0      wrapDatabaseException (package:sqflite/src/exception_impl.dart:11:7)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): #1      SqfliteDatabaseMixin.txnRawInsert.<anonymous closure> (package:sqflite_common/src/database_mixin.dart:548:14)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): #2      BasicLock.synchronized (package:synchronized/src/basic_lock.dart:33:16)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): #3      SqfliteDatabaseMixin.txnSynchronized (package:sqflite_common/src/database_mixin.dart:489:14)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): #4      DatabaseHelper.saveRecipe (package:recipedia/Database/databaseHelper.dart:46:15)
E/flutter (28200): <asynchronous suspension>
E/flutter (28200): 

Here is my code Getting data from firestore database and saving it into local database (sqflite)

  Future<void> getRecipeData() async {
    recipes = await RecipeModel().getRecipeDataList();
    count = await RecipeModel().getRecipeCount();

    //Converting List into Map
    for (int i=0; i<count;i++) {
      recipeMap[recipes[i].recipeID] = {
        'recipeName': recipes[i].recipeName,
        'recipeCategory': recipes[i].recipeCategory,
      };
    }
    //Storing data into database
    DatabaseHelper().saveRecipe(recipeMap);
  }

This is my database class

class DatabaseHelper {
  static final DatabaseHelper _instance = DatabaseHelper.internal();

  factory DatabaseHelper() => _instance;

  final String recipeTable = 'recipeTable';
  final String recipeId = 'recipeId';
  final String recipeName = 'recipeName';
  final String recipeCategory = 'recipe_category';
  final String columnSynced = 'synced';

  static Database? _db;

  Future<Database?> get db async {
    if (_db != null) {
      return _db;
    }
    _db = await initDb();
    return _db;
  }

  DatabaseHelper.internal();

  initDb() async {
    String databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'recipe.db');

    var db = await openDatabase(path, version: 1, onCreate: _onCreate);
    return db;
  }

  void _onCreate(Database db, int newVersion) async {
    await db.execute(
        'CREATE TABLE $recipeTable($recipeId INTEGER PRIMARY KEY, $recipeName TEXT, $recipeCategory TEXT, $columnSynced INTEGER)');
  }

  Future<int> saveRecipe(Map<String, dynamic> recipe) async {
    var dbClient = await db;
    int res = await dbClient!.insert(recipeTable, recipe);
    return res;
  }
}

Please help me to resolve the issue. All the efforts in question will be appreciated. Thankyou

Ahmed Raza
  • 29
  • 6

1 Answers1

0

You are trying to insert Map data into SQLite Database in your method saveRecipe.

As Save Hashmap Data Into SQLite and How to Convert Hashmap to String to Save in SQLite Database post explains; you need to convert your recipe Map to a string / json to store in your SQLite Database.

You can change the saveRecipe method to :

Future<int> saveRecipe(Map<String, dynamic> recipe) async {
  var dbClient = await db;
  String recipeJson = jsonEncode(recipe); 
  int res = await dbClient!.insert(recipeTable, {'recipe': recipeJson});
  return res;
}
Andy Preston
  • 779
  • 4
  • 9
  • 23
Tushar
  • 3,527
  • 9
  • 27
  • 49
  • This is the error it shows me after applying your solution `E/SQLiteLog( 5185): (1) table recipeTable has no column named recipe in "INSERT INTO recipeTable (recipe) VALUES (?)" E/flutter ( 5185): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(table recipeTable has no column named recipe (code 1 SQLITE_ERROR): , while compiling: INSERT INTO recipeTable (recipe) VALUES (?)) sql 'INSERT INTO recipeTable (recipe) VALUES (?)' args [{"101":{"recipeName":"Butter Chicken","recipeCateg...]` – Ahmed Raza Mar 03 '23 at 10:01
  • Can you update the line to : `int res = await dbClient!.insert(recipeTable, recipeJson);` And what error you get ? – Tushar Mar 03 '23 at 10:29
  • 1
    When I update the above line there is a red underline on recipeJson and it shows compilation error `The argument type 'String' can't be assigned to the parameter type 'Map'` – Ahmed Raza Mar 03 '23 at 10:32
  • 1
    Correct, A compilation error – Tushar Mar 03 '23 at 10:33
  • 1
    Looking at your `DatabaseHelper` and `onCreate` method; which has 'recipeName' and `recipeCategory`; I will check how to resolve this – Tushar Mar 03 '23 at 10:40
  • Also, Can you use either `recipeCategory` or `recipe_Category` in your code ? The names are different in `DatabaseHelper` and `onCreate` method – Tushar Mar 03 '23 at 10:42
  • I made it `recipeCategory` in both `DatabaseHelper` and `onCreate` method – Ahmed Raza Mar 03 '23 at 10:50
  • Can you use the solution in answer and tell me; do you still get the same error ? – Tushar Mar 03 '23 at 11:17
  • 1
    Yes the same error, Exactly same error as in first comment – Ahmed Raza Mar 03 '23 at 11:23