I used Moor For storing my local data, and it's working well but today when I do some changes in my UI, I got exception in Moor Method, I don't know why it's giving exception because it's working tomorrow without exception.
My Exception:
Here is my moor_database.dart:
import 'package:moor_flutter/moor_flutter.dart';
part 'moor_database.g.dart';
class Passwords extends Table{
IntColumn get id => integer().autoIncrement()(); // autoIncrement automatically sets this to be the primary key
TextColumn get wName => text()();
TextColumn get wAddress => text()();
TextColumn get uName => text()();
TextColumn get password => text()();
TextColumn get notes => text()();
}
class CardDetails extends Table{
IntColumn get id => integer().autoIncrement()();
TextColumn get cName => text()();
TextColumn get cNumber => text()();
TextColumn get uName => text()();
TextColumn get expiration => text()();
TextColumn get cvv => text()();
TextColumn get pin => text()();
TextColumn get notes => text()();
}
class Banks extends Table{
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text()();
TextColumn get bName => text()();
TextColumn get aNumber => text()();
TextColumn get aType => text()();
TextColumn get ifsc => text()();
TextColumn get branchName => text()();
TextColumn get branchAddress => text()();
TextColumn get bNumber => text()();
TextColumn get notes => text()();
}
class Notes extends Table{
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text()();
TextColumn get notes => text()();
}
@UseMoor(tables: [Passwords, CardDetails, Banks, Notes], daos: [PasswordDao, CardDetailDao, BankDao, NoteDao])
class AppDatabase extends _$AppDatabase {
AppDatabase()
: super(FlutterQueryExecutor.inDatabaseFolder(
path: 'db.sqlite', logStatements: true));
@override
int get schemaVersion => 1;
}
@UseDao(tables: [Passwords])
class PasswordDao extends DatabaseAccessor<AppDatabase> with _$PasswordDaoMixin{
final AppDatabase db;
PasswordDao(this.db) : super(db);
Future<List<Password>> getAllPasswords() => select(passwords).get();
Stream<List<Password>> watchAllPasswords() {
return (select(passwords)
..orderBy([
(t) => OrderingTerm(expression: t.wName)
])
)
.watch();
}
Future insertPassword(Insertable<Password> password) => into(passwords).insert(password);
Future updatePassword(Insertable<Password> password) => update(passwords).replace(password);
Future deletePassword(Insertable<Password> password) => delete(passwords).delete(password);
}
@UseDao(tables: [CardDetails])
class CardDetailDao extends DatabaseAccessor<AppDatabase> with _$CardDetailDaoMixin{
final AppDatabase db;
CardDetailDao(this.db) : super(db);
Future<List<CardDetail>> getAllCards() => select(cardDetails).get();
Stream<List<CardDetail>> watchAllCards() {
return (select(cardDetails)
..orderBy([
(t) => OrderingTerm(expression: t.cName)
])
)
.watch();
}
Future insertCardDetail(Insertable<CardDetail> cardDetail) => into(cardDetails).insert(cardDetail);
Future updateCardDetail(Insertable<CardDetail> cardDetail) => update(cardDetails).replace(cardDetail);
Future deleteCardDetail(Insertable<CardDetail> cardDetail) => delete(cardDetails).delete(cardDetail);
}
@UseDao(tables: [Banks])
class BankDao extends DatabaseAccessor<AppDatabase> with _$BankDaoMixin{
final AppDatabase db;
BankDao(this.db) : super(db);
Future<List<Bank>> getAllBanks() => select(banks).get();
Stream<List<Bank>> watchAllBanks() {
return (select(banks)
..orderBy([
(t) => OrderingTerm(expression: t.bName)
])
)
.watch();
}
Future insertBank(Insertable<Bank> bank) => into(banks).insert(bank);
Future updateBank(Insertable<Bank> bank) => update(banks).replace(bank);
Future deleteBank(Insertable<Bank> bank) => delete(banks).delete(bank);
}
@UseDao(tables: [Notes])
class NoteDao extends DatabaseAccessor<AppDatabase> with _$NoteDaoMixin{
final AppDatabase db;
NoteDao(this.db) : super(db);
Future<List<Note>> getAllNotes() => select(notes).get();
Stream<List<Note>> watchAllNotes() {
return (select(notes)
..orderBy([
(t) => OrderingTerm(expression: t.title)
])
)
.watch();
}
Future insertNote(Insertable<Note> note) => into(notes).insert(note);
Future updateNote(Insertable<Note> note) => update(notes).replace(note);
Future deleteNote(Insertable<Note> note) => delete(notes).delete(note);
}
Here is my UI code where I am showing listView:
import 'package:flutter/material.dart';
import 'package:moor_flutter/moor_flutter.dart' hide Column;
import 'package:provider/provider.dart';
import 'package:secret_keeper/Database/Moor/moor_database.dart';
import 'package:secret_keeper/screens/home_screen/passwords/ShowData.dart';
class PasswordsNavigation extends StatefulWidget {
@override
_PasswordsNavigationState createState() => _PasswordsNavigationState();
}
class _PasswordsNavigationState extends State<PasswordsNavigation> {
String _wName, _wAddress, _userName, _password, _notes;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: _buildPasswordList(context),
),
],
),
);
}
Widget stackBehindDismiss() {
return Container(
alignment: Alignment.centerRight,
padding: EdgeInsets.only(right: 20.0),
color: Colors.red,
child: Icon(
Icons.delete,
color: Colors.white,
),
);
}
StreamBuilder<List<Password>> _buildPasswordList(BuildContext context) {
final dao = Provider.of<PasswordDao>(context);
return StreamBuilder(
stream: dao.watchAllPasswords(),
builder: (context, AsyncSnapshot<List<Password>> snapshot) {
final passwords = snapshot.data ?? List();
return ListView.builder(
itemCount: passwords.length,
itemBuilder: (_, index) {
final itemPassword = passwords[index];
return _buildListItem(itemPassword, dao);
},
);
},
);
}
Widget _buildListItem(Password itemPassword, PasswordDao dao) {
return Dismissible(
background: stackBehindDismiss(),
key: ObjectKey(itemPassword),
direction: DismissDirection.endToStart,
onDismissed: (direction) {
_wName = itemPassword.wName;
_wAddress = itemPassword.wAddress;
_userName = itemPassword.uName;
_password = itemPassword.password;
_notes = itemPassword.notes;
dao.deletePassword(itemPassword);
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text("Entry deleted!"),
action: SnackBarAction(
label: "UNDO",
textColor: Color(0xFFFFFFFF),
onPressed: () {
//To undo deletion
undoDeletion(_wName, _wAddress, _userName, _password, _notes);
},
),
),
);
},
child: ListTile(
title: Text(itemPassword.wName),
subtitle: Text(itemPassword.wAddress),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => ShowData(
itemPassword: itemPassword,
),
),
);
},
),
);
}
void undoDeletion(wName, wAddress, userName, password, notes) {
final passwordDao = Provider.of<PasswordDao>(context);
final values = PasswordsCompanion(
wName: Value(wName),
wAddress: Value(wAddress),
uName: Value(userName),
password: Value(password),
notes: Value(notes)
);
passwordDao.insertPassword(values);
}
}
Here is my pubspec.yaml:
name: secret_keeper
description: This app is used to store your secrete passwords, bank details and much more..
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
moor_flutter: ^1.6.0
# For the UI
provider: ^3.0.0+1
fluttertoast: ^7.1.5
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0
path_provider: ^1.6.5
dev_dependencies:
flutter_test:
sdk: flutter
email_validator: ^1.0.0
moor_generator: ^1.6.0
build_runner:
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
fonts:
- family: GoogleFonts
fonts:
- asset: assets/fonts/BaiJamjuree-Bold.ttf
- asset: assets/fonts/BaiJamjuree-BoldItalic.ttf
- asset: assets/fonts/BaiJamjuree-ExtraLight.ttf
- asset: assets/fonts/BaiJamjuree-ExtraLightItalic.ttf
- asset: assets/fonts/BaiJamjuree-Italic.ttf
- asset: assets/fonts/BaiJamjuree-Light.ttf
- asset: assets/fonts/BaiJamjuree-LightItalic.ttf
- asset: assets/fonts/BaiJamjuree-Medium.ttf
- asset: assets/fonts/BaiJamjuree-MediumItalic.ttf
- asset: assets/fonts/BaiJamjuree-Regular.ttf
- asset: assets/fonts/BaiJamjuree-SemiBold.ttf
- asset: assets/fonts/BaiJamjuree-SemiBoldItalic.ttf
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
User contributions licensed under CC BY-SA 3.0