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 :
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?>'.
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]);
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → I can't do a foreign key, constraint error
- → Setting a default value on settings form return null in Octobercms
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Image does not go in database with file name only tmp name saved?
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Trait 'IlluminateFoundationBusDispatchesJobs' not found
- → Setting the maxlength of text in an element that is displayed
- → laravel check between 2 integer from database
- → how to retrieve image from database in laravel 5.1?
- → relationship for database Column type object
- → Carousel in Laravel 4 does not show as expected