2
respostas

Erro ao criar banco sqlite

E/flutter (16604): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method getDatabasesPath on channel com.tekartik.sqflite) E/flutter (16604): #0 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)

void main(){
  runApp(BytebankApp());
  save(Contact(0,'Ana Beatriz', 3000)).then((id){
    findAll().then((contacts) => debugPrint(contacts.toString()));
  });
}
import 'package:baytebank/models/contact.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

Future<Database> createDatabase() {
  return getDatabasesPath().then((dbPath) {
    final String path = join(dbPath, 'bytebank.db');
    return openDatabase(path, onDowngrade: onDatabaseDowngradeDelete,
        onCreate: (db, version) {
      db.execute('CREATE TABLE contacts('
          'id INTEGER PRIMARY KEY, '
          'name TEXT, '
          'account_number INTEGER)');
    }, version: 1);
  });
}

Future<int> save(Contact contact) {
  return createDatabase().then((db) {
    final Map<String, dynamic> contactMap = Map();
    contactMap['name'] = contact.name;
    contactMap['account_number'] = contact.accountNumber;
    return db.insert('contacts', contactMap);
  });
}

Future<List<Contact>> findAll() {
  return createDatabase().then((db) {
    return db.query('contacts').then((maps) {
      final List<Contact> contacts = List();
      for (Map<String, dynamic> map in maps) {
        final Contact contact =
            Contact(map['id'], map['name'], map['account_number']);
        contacts.add(contact);
      }
      return contacts;
    });
  });
}

2 respostas

Estou com o mesmo problema. Conseguiu resolver?

Consegui resolver através dos seguntes passos:

  • Parar o app
  • Acessar o menu Tools > Flutter > Flutter Clean
  • Buildar novamente
  • Executar a aplicação.

Após estes passos, funcionou normalmente para mim.