Begin working on the database.

This commit is contained in:
Hyperling 2025-02-05 14:43:06 -07:00
parent 6b25e6e552
commit 5561f50736
3 changed files with 52 additions and 3 deletions

45
lib/db.dart Normal file
View 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,
);
}

View File

@ -1,15 +1,16 @@
// Helpful guides:
// - https://flutter.dev/docs/cookbook/forms/validation
// Flutter
import 'package:flutter/material.dart';
// Local
import '/pages/expense.dart';
import '/pages/income.dart';
import '/pages/asset.dart';
import '/pages/report.dart';
import '/pages/settings.dart';
import '/db.dart';
void main() {
loadDB();
runApp(const MainApp());
}

View File

@ -1,5 +1,7 @@
// Flutter
import 'package:flutter/material.dart';
// Local
import '/models/expense.dart';
import '/models/frequency.dart';
@ -61,6 +63,7 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
@override
Widget build(BuildContext context) {
// TODO: Do this as a values.map().
List<DropdownMenuItem> freqValues = [];
for (var freq in Frequency.values) {
freqValues.add(DropdownMenuItem(value: freq, child: Text(freq.title)));