SQLite DB Table not found by custom action

Database & APIs

I'm developing an android app that scans SMSs and extracts pieces of information from them and places it in a SQLite DB. I have a button that is linked to the below custom code

// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_util.dart';
// Imports other custom actions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

// Import necessary packages
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:flutter_sms_inbox/flutter_sms_inbox.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:intl/intl.dart';
import 'package:sqflite/sqflite.dart';

// Generate MD5 hash from a string
String generateMd5(String input) {
  return md5.convert(utf8.encode(input)).toString();
}

Future<bool> fetchAndSaveSms(BuildContext context) async {
  // Request SMS permission
  var permission = await Permission.sms.status;
  if (permission != PermissionStatus.granted) {
    permission = await Permission.sms.request();
    if (permission != PermissionStatus.granted) {
      print('SMS permission denied');
      return false; // Scanning not complete
    }
  }

  // Initialize SQLite database and table
  final Future<Database> database = openDatabase(
    'MONEY.db',
    version: 1,
  );

  // Fetch SMS messages from "COMBANK"
  SmsQuery query = SmsQuery();
  List<SmsMessage> messages = await query.querySms(
    kinds: [SmsQueryKind.inbox], // Fetch only inbox messages
    address: 'COMBANK', // Filter messages from "COMBANK"
  );

  // Limit to the first 10 messages
  messages = messages.take(10).toList();

  // Save filtered messages to SQLite
  for (SmsMessage message in messages) {
    if (message.sender?.contains('COMBANK') ?? false) {
      // Extract sender (phone number or name)
      String sender = message.sender ?? 'Unknown sender';

      // Extract numerical values from body
      String numericBody =
          message.body?.replaceAll(RegExp(r'[^0-9]'), '') ?? '';

      // Get current timestamp
      String timestamp = DateTime.now().millisecondsSinceEpoch.toString();

      // Calculate MD5 hash (with timestamp)
      String md5Hash = generateMd5('$sender$numericBody$timestamp');

      // Format the DateTime object to "yyyy/MM/dd" for display purposes
      String formattedDate = DateFormat('yyyy/MM/dd').format(DateTime.now());

      // Add message to SQLite with MD5 hash as message ID
      final db = await database;
      await db.insert(
        'AllMessages',
        {
          'MessageID': md5Hash,
          'MessageTime': formattedDate,
          'MessageSender': sender,
          'MessageBody': message.body ?? 'No body',
        },
      );
    }
  }

  // Set the scanning completion flag
  bool isScanningComplete = true;

  // Return the flag to indicate scanning completion
  return isScanningComplete;
}

The first screenshot has the device log messages that I see when i try to run the app via the flutterflow desktop app (see below)

I can confirm that the table actually does have the tables. (see below)

Below is my main.dart file

import 'package:flutter/material.dart';

import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import '/backend/sqlite/sqlite_manager.dart';
import 'backend/firebase/firebase_config.dart';
import 'flutter_flow/flutter_flow_theme.dart';
import 'flutter_flow/flutter_flow_util.dart';
import 'flutter_flow/nav/nav.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  GoRouter.optionURLReflectsImperativeAPIs = true;
  usePathUrlStrategy();
  await initFirebase();

  await SQLiteManager.initialize();
  await FlutterFlowTheme.initialize();

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  State<MyApp> createState() => _MyAppState();

  static _MyAppState of(BuildContext context) =>
      context.findAncestorStateOfType<_MyAppState>()!;
}

class _MyAppState extends State<MyApp> {
  ThemeMode _themeMode = FlutterFlowTheme.themeMode;

  late AppStateNotifier _appStateNotifier;
  late GoRouter _router;

  @override
  void initState() {
    super.initState();

    _appStateNotifier = AppStateNotifier.instance;
    _router = createRouter(_appStateNotifier);
  }

  void setThemeMode(ThemeMode mode) => setState(() {
        _themeMode = mode;
        FlutterFlowTheme.saveThemeMode(mode);
      });

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Mon-EX',
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [Locale('en', '')],
      theme: ThemeData(
        brightness: Brightness.light,
        useMaterial3: false,
      ),
      darkTheme: ThemeData(
        brightness: Brightness.dark,
        useMaterial3: false,
      ),
      themeMode: _themeMode,
      routerConfig: _router,
    );
  }
}

Would greatly appreciate some help in resolving this error :(

What have you tried so far?

I have tried uninstalling the app before testing
I have cleared storage and then uninstalling and testing
I've only being testing the app on the flutterflow windows programme and vscode.
I have tried creating simple textfields and listview combinations to update and view the data in the SQLite DB using actions linked to a button press (it works, which means the SQLite DB and the tables are functioning as intended.)
I've gone through the official documentation regarding SQLite and SQFLite to no avail.

Did you check FlutterFlow's Documentation for this topic?
Yes
4
1 reply