'can't save contacts to local db in flutter
I'm trying to save the contacts from phone to loacal db using sqflite in flutter but getting error in saving the data from contacts to sql table.
I don't understand how to save data from Contact_service to localdb.I have used contact_service package to get the contacts from phone.
import 'dart:io';
import 'package:contacts_service/contacts_service.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
class ContactDatabase {
static final _databaseName = "contact.db";
static final _databaseVersion = 1;
List<Contact> contact = [];
ContactDatabase._init();
static Database _database;
static final ContactDatabase instance = ContactDatabase._init();
Future<Database> get database async {
if (_database != null) return _database;
_database = await _initDB();
return _database;
}
_initDB() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabase(path,
version: _databaseVersion, onCreate: _onCreate);
}
static final contact_table = 'contacts';
static final id = 'id';
static final name = 'name';
static final phone = 'phone';
static final avatar = 'avatar';
static final synced = 'synced';
static final CREATE_TABLE = "CREATE TABLE $contact_table"
"($name TEXT ,"
"$phone TEXT ,"
"$avatar TEXT ,"
"$synced INTEGER DEFAULT '0' ,"
"$id INTEGER PRIMARY KEY AUTOINCREMENT)";
Future _onCreate(Database db, int version) async {
await db.execute('$CREATE_TABLE');
}
Future<int> insert(table, Map<String, dynamic> row) async {
Database db = await instance.database;
return await db.insert(table, row,
conflictAlgorithm: ConflictAlgorithm.replace);
}
Future<void> inserContact() async {
contact = (await ContactsService.getContacts()).toList();
for (var i = 0; i < 10; i++) {
Map<String, dynamic> row = {
name: contact[i].displayName,
phone: contact[i].displayName,
avatar: "NA",
synced: 1,
};
insert(contact_table, row);
}
}
Future<List<Map<String, dynamic>>> queryAllRows() async {
Database db = await instance.database;
return await db.query(contact_table);
}
}
can anyone give me a solution
Solution 1:[1]
Future<void> insertContact() async {
if (!await FlutterContacts.requestPermission(readonly: true)) {
setState(() => _permissionDenied = true);
} else {
final contacts = await FlutterContacts.getContacts();
setState(() => _contacts = contacts);
for (var i = 0; i <= _contacts!.length; i++) {
final fullContact = await FlutterContacts.getContact(_contacts![i].id);
if (fullContact!.phones.first.normalizedNumber.isNotEmpty &&
fullContact!.phones.first.normalizedNumber.replaceAll(' ', '').length > 8) {
savecontact(userid, _contacts![i].displayName, fullContact.phones.first.normalizedNumber);
}
}
}
}
with flutter_contacts i solved it that way to save the number in the database
hope it helps the community etc.
but getting an error:
Unhandled Exception: RangeError (index): Invalid value: Not in inclusive range
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
