Begin working on the database.
This commit is contained in:
parent
6b25e6e552
commit
5561f50736
45
lib/db.dart
Normal file
45
lib/db.dart
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// https://docs.flutter.dev/cookbook/persistence/sqlite
|
||||||
|
|
||||||
|
// SQLite
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:flutter_expense_tracker/models/frequency.dart';
|
||||||
|
import 'package:path/path.dart';
|
||||||
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
|
||||||
|
// Local
|
||||||
|
import '/models/expense.dart';
|
||||||
|
|
||||||
|
void loadDB() async {
|
||||||
|
// Avoid errors caused by flutter upgrade.
|
||||||
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
final String frequencies =
|
||||||
|
"'${Frequency.values.map((freq) => freq.title).join("','")}'";
|
||||||
|
print(frequencies);
|
||||||
|
|
||||||
|
// Open the database and store the reference.
|
||||||
|
final database = openDatabase(
|
||||||
|
// Set the path to the database. Note: Using the `join` function from the
|
||||||
|
// `path` package is best practice to ensure the path is correctly
|
||||||
|
// constructed for each platform.
|
||||||
|
join(await getDatabasesPath(), 'expense_tracker.db'),
|
||||||
|
|
||||||
|
onCreate: (db, version) {
|
||||||
|
// Run the CREATE TABLE statement on the database.
|
||||||
|
return db.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE expense
|
||||||
|
( id INTEGER PRIMARY KEY
|
||||||
|
, name TEXT
|
||||||
|
, cost DOUBLE
|
||||||
|
, frequency TEXT CHECK(frequency IN ($frequencies) )
|
||||||
|
, description TEXT
|
||||||
|
)""",
|
||||||
|
);
|
||||||
|
},
|
||||||
|
// Set the version. This executes the onCreate function and provides a
|
||||||
|
// path to perform database upgrades and downgrades.
|
||||||
|
version: 1,
|
||||||
|
);
|
||||||
|
}
|
@ -1,15 +1,16 @@
|
|||||||
// Helpful guides:
|
// Flutter
|
||||||
// - https://flutter.dev/docs/cookbook/forms/validation
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
// Local
|
||||||
import '/pages/expense.dart';
|
import '/pages/expense.dart';
|
||||||
import '/pages/income.dart';
|
import '/pages/income.dart';
|
||||||
import '/pages/asset.dart';
|
import '/pages/asset.dart';
|
||||||
import '/pages/report.dart';
|
import '/pages/report.dart';
|
||||||
import '/pages/settings.dart';
|
import '/pages/settings.dart';
|
||||||
|
import '/db.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
loadDB();
|
||||||
runApp(const MainApp());
|
runApp(const MainApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
// Flutter
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
// Local
|
||||||
import '/models/expense.dart';
|
import '/models/expense.dart';
|
||||||
import '/models/frequency.dart';
|
import '/models/frequency.dart';
|
||||||
|
|
||||||
@ -61,6 +63,7 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
// TODO: Do this as a values.map().
|
||||||
List<DropdownMenuItem> freqValues = [];
|
List<DropdownMenuItem> freqValues = [];
|
||||||
for (var freq in Frequency.values) {
|
for (var freq in Frequency.values) {
|
||||||
freqValues.add(DropdownMenuItem(value: freq, child: Text(freq.title)));
|
freqValues.add(DropdownMenuItem(value: freq, child: Text(freq.title)));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user