Flutter:遇到类似错误 - 参数类型 'Map<String, dynamic>?' 无法分配给参数类型 'Map<String, Object?>'
Flutter : Facing an error like - The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, Object?>'
我正在练习 flutter SQFLite。这就是我为用户信息创建模型的原因。这是我的模型代码-
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]);
}
然后我创建了一个数据库助手,用于从数据库中插入和获取数据。但是我在插入值时遇到了问题(参数类型 'Map<dynamic, dynamic>?' 不能分配给参数类型 'Map<String, Object?>' )。这是我的数据库助手代码 -
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();
}
}
错误:
我的问题在哪里?我错过了什么?请有人帮我解决这个问题。
更新:
我更改参数类型“Map?”到“地图<字符串,动态>?”但现在我发现了另一个错误。
The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, Object?>'.
最近的错误:
改变
Map<dynamic, dynamic>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
至
Map<String, dynamic>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
您收到此错误是因为在插入函数中,地图类型是 <String, Object?
> 而您传递的地图类型是 <dynamic, dynamic>
。尝试改变
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]);
至
//You don't need to pass id because it's auto incremented
[ 为空安全使用 Map<String, Object?>
而不是 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]);
你的数据库说他想要一张像
这样的地图
Map<String, Object>
此处地图的键必须是 String
并且您传递的地图具有 dynamic
类型的键
所以将地图的键从 dynamic
更改为 String
更改此地图类型
Map<dynamic, dynamic>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
至
Map<String, dynamic>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
之后你会得到另一个error
喜欢
The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, Object?>'.
现在我们首先要理解Map<String, Object?>
是什么意思?
类型 Object
是您的地图值,必须是特定类型的数据,例如 int
double
String
或任何 Custom Object
因为 SQL 数据库将数据存储在列中,每列必须保持特定的数据类型,所以尝试这样
Map<String, Object>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
我正在练习 flutter SQFLite。这就是我为用户信息创建模型的原因。这是我的模型代码-
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]);
}
然后我创建了一个数据库助手,用于从数据库中插入和获取数据。但是我在插入值时遇到了问题(参数类型 'Map<dynamic, dynamic>?' 不能分配给参数类型 'Map<String, Object?>' )。这是我的数据库助手代码 -
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();
}
}
错误:
我的问题在哪里?我错过了什么?请有人帮我解决这个问题。
更新:
我更改参数类型“Map
The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, Object?>'.
最近的错误:
改变
Map<dynamic, dynamic>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
至
Map<String, dynamic>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
您收到此错误是因为在插入函数中,地图类型是 <String, Object?
> 而您传递的地图类型是 <dynamic, dynamic>
。尝试改变
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]);
至
//You don't need to pass id because it's auto incremented
[ 为空安全使用 Map<String, Object?>
而不是 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]);
你的数据库说他想要一张像
这样的地图Map<String, Object>
此处地图的键必须是 String
并且您传递的地图具有 dynamic
类型的键
所以将地图的键从 dynamic
更改为 String
更改此地图类型
Map<dynamic, dynamic>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
至
Map<String, dynamic>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};
之后你会得到另一个error
喜欢
The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, Object?>'.
现在我们首先要理解Map<String, Object?>
是什么意思?
类型 Object
是您的地图值,必须是特定类型的数据,例如 int
double
String
或任何 Custom Object
因为 SQL 数据库将数据存储在列中,每列必须保持特定的数据类型,所以尝试这样
Map<String, Object>? toMap() => {
"id": colId,
"name": colName.toString(),
"mobile": colMobile.toString(),
};