I have an app in flutter want to add list of data to sqlite database at initializing the database ,I have problem with the type of model. I have this model for data :
import 'dart:convert';
List<Clubs> clubsFromMap(String str) =>
List<Clubs>.from(json.decode(str).map((x) => Clubs.fromMap(x)));
String clubsToMap(List<Clubs> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
class Clubs {
Clubs({
this.id,
this.club,
this.leagueId,
this.price,
this.surname,
this.leagueName,
this.counter,
this.selected,
});
int id;
String club;
int leagueId;
String price;
String surname;
String leagueName;
int counter;
String selected;
factory Clubs.fromMap(Map<String, dynamic> json) => Clubs(
id: json["id"],
club: json["club"],
leagueId: json["league_id"],
price: json["price"],
surname: json["surname"],
leagueName: json["league_name"],
counter: json["counter"],
selected: json["selected"],
);
Map<String, dynamic> toMap() => {
"id": id,
"club": club,
"league_id": leagueId,
"price": price,
"surname": surname,
"league_name": leagueName,
"counter": counter,
"selected": selected,
};
}
and I have this list of data for that model :
var clubs = [
{
"id": 1,
"club": "Manchester City",
"league_id": 1,
"price": "10.00",
"surname": "MCY",
"league_name": "Premier League",
"counter": 1,
"selected": "No"
},
..................etc
]
no I want tho add this initial data to sqflite database ,I created thsi :
import 'package:sqflite/sqflite.dart';
class DataBaseService {
static final DataBaseService _instance = DataBaseService.internal();
factory DataBaseService() => _instance;
DataBaseService.internal();
Database _database;
Future<Database> get database async {
if (_database == null) {
_database = await intializeDataBase();
return _database;
}
}
Future<Database> intializeDataBase() async {
var dir = await getDatabasesPath();
var path = dir + "clubs.db";
var database =
await openDatabase(path, version: 1, onCreate: (db, version) {
db.execute('''
create table $clubsTableName(
columnId integer primary key,
$columnClub text not null,
$columnLeaueId integer,
$columnPrice double,
$columnSurname text not null,
$columnLeagueName text,
$columnCounter integer.
$columnSelected text,
)
''');
db.insert(clubsTableName,clubs.toMap());
it say that toMap() isn't defined ,if I changed it to clubsFromMap(clubs) instead of clubs.toMap() it says : The argument type 'List<Clubs>' can't be assigned to the parameter type 'Map<String, Object>'.dart(argument_type_not_assignable)
How can I solve this?
I solved it after change the list format like this:
var clubs = {"data":{
{
"id": 1,
"club": "Manchester City",
"league_id": 1,
"price": "10.00",
"surname": "MCY",
"league_name": "Premier League",
"counter": 1,
"selected": "No"
},
..................etc}
}