Ad

Flutter : Facing An Error Like - The Argument Type 'Map?' Can't Be Assigned To The Parameter Type 'Map'

I am practicing with flutter SQFLite. That's why I create a model for user info. Here is my model code-

    class Contact {
      static const tblContact = "contacts";
      static const colId = "id";
      static const colName = "name";
      static const colMobile = "mobile";
    
      Contact({
        this.id,
        this.name = '',
        this.mobile = '',
      });
    
      int? id;
      String name;
      String mobile;
    
        Map<String, dynamic>? toMap() => {
        "id": colId,
        "name": colName.toString(),
        "mobile": colMobile.toString(),
      };

  factory Contact.fromMap(Map<String, dynamic> json) =>
      Contact(name: json[colName], mobile: json[colMobile]);
    }

and then I create a database helper for insert and fetch data from database. But I faced a problem to insert value ( The argument type 'Map<dynamic, dynamic>?' can't be assigned to the parameter type 'Map<String, Object?>' ). Here is my database helper code -

import 'dart:io';
......
.......

class DatabaseHelper {
  static const _databaseName = "ContactData.db";
  static const _databaseVersion = 1;

//<====== Singleton Class
  DatabaseHelper._();
  static final DatabaseHelper instance = DatabaseHelper._();

  Database? _database;
  Future<Database?> get database async {
    if (_database != null) {
      return _database;
    } else {
      _database = await _initDatabase();
      return _database;
    }
  }

//CREATE DATABASE
  _initDatabase() async {
    Directory dataDirectory = await getApplicationDocumentsDirectory();
    String dbPath = join(dataDirectory.path, _databaseName);
    print(dbPath);
    return await openDatabase(dbPath,
        version: _databaseVersion, onCreate: _onCreate);
  }

  //CREATE TABLE
  _onCreate(Database db, int version) async {
    db.execute(''' 
CREATE TABLE ${Contact.tblContact}(
  ${Contact.colId} INTEGER PRIMARY KEY AUTOINCREMENT,
  ${Contact.colName} STRING NOT NULL,
  ${Contact.colMobile} STRING NOT NULL
);
''');
    print("Done on Create");
  }

  //<===================  ADD DATA

  Future<int> insertContact(Contact contact) async {
    Database? db = await database;

    return await db!.insert(Contact.tblContact, contact.toMap());
  }

  //<==================== Read Data
  Future<List<Contact>> fetchContacts() async {
    Database? db = await database;
    List<Map<String, dynamic>> contacts = await db!.query(Contact.tblContact);
    print("Done Fetch");
    return contacts.length == 0
        ? []
        : contacts.map((x) => Contact.fromMap(x)).toList();
  }
}

Error :

enter image description here

Where is my problem and what I missed ? Please someone help me to solve this.

Update: I change argument type "Map<dynamic, dynamic>?" to "Map<String, dynamic>?" but now I found another error .

The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, Object?>'.

Recent Error:enter image description here

Ad

Answer

You are getting this error because in insert function the Map type is <String, Object?> and you are passing a map that's type is <dynamic, dynamic>. try changing

Map<dynamic, dynamic>? toMap() => {
        "id": colId,
        "name": colName.toString(),
        "mobile": colMobile.toString(),
      };

  factory Contact.fromMap(Map<dynamic, dynamic> json) =>
      Contact(name: json[colName], mobile: json[colMobile]); 

to

//You don't need to pass id because it's auto incremented

[ for null safety Use Map<String, Object?> not Map<String, Object>?]

Map<String, Object?> toMap() => {
        "name": colName.toString(),
        "mobile": colMobile.toString(),
      };

  factory Contact.fromMap(Map<String, Object?> json) =>
      Contact(name: json[colName], mobile: json[colMobile]);
Ad
source: stackoverflow.com
Ad