Search code examples
flutterdartandroid-sqlite

Sqlite syntax error in Create table statement - flutter/dart


I keep getting this syntax error : E/SQLiteLog( 4514): (1) near "null": syntax error in "CREATE TABLE expenses_table(id INTEGER PRIMARY KEY AUTOINCREMENT, null TEXT ,null TEXT)"

I am quite new to this, and have been using examples online to put this code together, but I can't seem to find what is causing this issue.

Any help would be very much appreciated!

'''

import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'expense_list.dart';

class ExpensesDatabase {
  static ExpensesDatabase _expensesDatabase;
  static Database _database;

  String expensesTable = 'expenses_table';

  String id = 'id';
  String name;
  String amount;

  ExpensesDatabase._createInstance();

  factory ExpensesDatabase() {
    if (_expensesDatabase == null) {
      _expensesDatabase = ExpensesDatabase._createInstance();
    }
    return _expensesDatabase;
  }

  Future<Database> get database async {
    if (_database == null) {
      _database = await initializeDatabase();
    }
    return _database;
  }

  Future<Database> initializeDatabase() async {
    Directory directory = await getApplicationDocumentsDirectory();
    String path = directory.path + 'expenses.db';

    var expensesDatabase =
        await openDatabase(path, version: 1, onCreate: _createDb);
    return expensesDatabase;
  }

  void _createDb(Database db, int newVersion) async {
    await db.execute(
        'CREATE TABLE $expensesTable($id INTEGER PRIMARY KEY AUTOINCREMENT,'
        '$name TEXT,'
        '$amount TEXT)');
  }

'''


Solution

  • Intialize your name and amount variables, its getting null because string name and amount are not initialised yet.

    String name = 'name';
    String amount = 'amount';