Using SQLite in Custom Functions or Custom Actions

Actions & Logic

Hi ,

I am developing an application using FlutterFlow, and I am having trouble using SQLite when creating custom functions or custom actions. I have noticed that I need values like opendatabase and dbpath every time. I would like to understand the issues I am facing in this process and the best approach to take.

import 'package:sqflite/sqflite.dart';

Future<Database> openDatabaseConnection() async {
  String dbPath = await getDatabasesPath();
  String path = join(dbPath, 'my_database.db');

  Database database = await openDatabase(
    path,
    version: 1,
    onCreate: (Database db, int version) async {
      await db.execute('''
        CREATE TABLE my_table (
          id INTEGER PRIMARY KEY,
          name TEXT
        )
      ''');
    },
  );

  return database;
}

Future<void> insertData(String name) async {
  Database db = await openDatabaseConnection();
  await db.insert('my_table', {'name': name});
}

Future<List<Map<String, dynamic>>> fetchData() async {
  Database db = await openDatabaseConnection();
  return await db.query('my_table');
}

In this code, the openDatabaseConnection function opens the database, the insertData function adds a new record, and the fetchData function retrieves existing data. However, how can I effectively implement these processes in FlutterFlow?

What best practices should I consider in this process? I would greatly appreciate your help!

Thank you!

What have you tried so far?

So far, I have set up a basic SQLite database connection using the sqflite package in my FlutterFlow project. I created a function to open the database and defined a table structure. Additionally, I implemented functions to insert and fetch data from the database. However, I am unsure how to integrate this setup effectively into the FlutterFlow environment, especially regarding the need for opendatabase and dbpath values every time I create a custom function or action.

I would appreciate any guidance on how to streamline this process or best practices for using SQLite within FlutterFlow.

Did you check FlutterFlow's Documentation for this topic?
Yes
3
2 replies