I hope you are all doing well !
I have ran into a problem concerning my Flutter app : I’ve been following a tutorial on Youtube on how to use sqflite in Flutter, and I couldn’t get my toMap function to work in my database class… It always returns me the error “Instance member “toMap” can’t be accessed using static access”. Does anyone know what this is about? I really don’t understand it… Thanks in advance !!
Down below, the problematic code :
poi.dart
final String tablePOI = 'poi'; class POIFields { static final String id = '_id'; static final String email = 'email'; static final String village = 'village'; static final String lieu = 'lieu'; static final String type = 'type'; static final String etat = 'etat'; static final String notes = 'notes'; static final String latitude = 'latitude'; static final String longitude = 'longitude'; static final String imageUrl = 'image_url'; } class POI { final int id; final String email; final String village; final String lieu; final String type; final String etat; final String notes; final String latitude; final String longitude; final String imageUrl; POI({this.id, this.email, this.village, this.lieu, this.type, this.etat, this.notes, this.latitude, this.longitude, this.imageUrl}); Map<String, Object> toMap() => { POIFields.id: id, POIFields.email: email, POIFields.village: village, POIFields.lieu: lieu, POIFields.type: type, POIFields.etat: etat, POIFields.notes: notes, POIFields.latitude: latitude, POIFields.longitude: longitude, POIFields.imageUrl: imageUrl, }; String toString() { return 'POI{id: $id, email: $email, village: $village, lieu: $lieu, type: $type, etat: $etat, notes: $notes, latitude: $latitude, longitude: $longitude, image_url: $imageUrl}'; } }
poi_db.dart
import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; import 'package:stage_app_mpc/models/poi.dart'; class POIDatabase { static final POIDatabase instance = POIDatabase._init(); static Database _database; POIDatabase._init(); Future<Database> get database async { if (_database != null) { return _database; } else { _database = await _initDB('poi.db'); return _database; } } Future<Database> _initDB(String filePath) async { final dbPath = await getDatabasesPath(); final path = join(dbPath, filePath); return openDatabase(path, version: 1, onCreate: _createDB); } Future _createDB(Database db, int version) async { await db.execute('CREATE TABLE poi (id INTEGER PRIMARY KEY, email TEXT, village TEXT, lieu TEXT, type TEXT, etat TEXT, notes TEXT, latitude TEXT, longitude TEXT, image_url TEXT)'); } Future<POI> create(POI poi) async { final db = await instance.database; final id = await db.insert('poi', POI.toMap()); } Future closeDB() async { final db = await instance.database; db.close(); } }
Advertisement
Answer
You are referencing the class POI
instead of the parameter poi
:
Future<POI> create(POI poi) async { final db = await instance.database; final id = await db.insert('poi', POI.toMap()); }
You should change it to reference the parameter that you are passing in.
Future<POI> create(POI poi) async { final db = await instance.database; final id = await db.insert('poi', poi.toMap()); }
NOTE: I would suggest, to avoid such confusions in the future, name your classes with less abbreviation. It helps to name them as descriptively as possible. Then for instanced variables, you can use shortened notation (though I would still try to name things in a descriptive manner to how it is being used).
In this case, whatever POI stands for: Place of Interest?
class PlaceOfInterest {...}
Then your parameter or field could be currentPoi
, activePoi
, poiData
, etc…