skip to Main Content

An error showed up while I was making an note app. I feel that I code everything correct but still I this error is showing:

"Error: Member not found: ‘NoteDatabaseHelper._initializeNoteDatabase’.
final Future dbFuture = NoteDatabaseHelper._initializeNoteDatabase();"

This is my note list file.

class ScreenNote extends StatefulWidget {
  @override
  _ScreenNote createState() => _ScreenNote();
}

class _ScreenNote extends State<ScreenNote> {
  late List<Note>? noteList;
  int count = 0;

  @override
  Widget build(BuildContext context) {
    if (noteList == null) {
      noteList = <Note>[];
      updateListView();
    }

    return ClipRRect(
        child: Slidable(child: GestureDetector(child: getNoteListView())));
  }

  ListView getNoteListView() {
    return ListView.builder(
      itemCount: count,
      itemBuilder: (BuildContext context, int position) {
        return Card(
          color: Colors.white,
          elevation: 2.0,
          child: Stack(
            children: [InkWell( onTap: () {
              editnote(this.noteList![position]);
            },),
              ListTile(
                title: Text(this.noteList![position].title as String),
                subtitle: Text(this.noteList![position].date as String),
              ),],
          )
        );
      },
    );
  }

  void editnote(Note note) async {
    bool result = await Navigator.push(context, MaterialPageRoute(builder: (context) {
      return NoteEditor(note);
    }));

    if (result == true) {
      updateListView();
    }
  }

  void updateListView() {
    final Future<Database> dbFuture = NoteDatabaseHelper._initializeNoteDatabase();
    dbFuture.then((database)                                      //error is shown here 
    {
      Future<List<Map<String, dynamic>>?> noteListFuture = NoteDatabaseHelper.queryAll();
      noteListFuture.then((noteList) {
        setState(() {
          this.noteList = noteList?.cast<Note>();
          this.count = noteList?.length as int;
        });
      });
    });
  }
}

This is my database helper file

class NoteDatabaseHelper {
  static final _dbName = 'noteDatabase.db';
  static final _dbVersion = 1;
  static final _tableName = 'noteTable';

  static final columnId = '_Id';
  static final columnTitle = '_Title';
  static final columnDescription = '_Description';
  static final columnDate = '_Date';

  NoteDatabaseHelper._privateConstructor();

  static final NoteDatabaseHelper instance =
      NoteDatabaseHelper._privateConstructor();

  static Database? _notedatabase;

  Future<Database?> get notedatabase async {
    if(notedatabase!=null){
      return _notedatabase;
    }
    else{
      _notedatabase = _initializeNoteDatabase() as Database?;
      return _notedatabase;
    }
  }

  Future<Database?> _initializeNoteDatabase() async{
    Directory directory = await getApplicationDocumentsDirectory();
    String path = join(directory.path, _dbName);
    return await openDatabase(path,version: _dbVersion, onCreate: _onCreate);
  }

  Future? _onCreate(Database db, int version){
    db.query(
      '''
      CREATE TABLE $_tableName(
      $columnId INTEGER PRIMARY KEY AUTOINCREMENT, 
      $columnTitle TEXT, 
            $columnDescription TEXT, 
            $columnDate TEXT)')
      '''
    );
  }

  static Future<int?> insert(Note note) async{
    Database? db = await instance.notedatabase;
    return await db?.insert(_tableName, note.toMap());
  }

  static Future<List<Map<String, dynamic>>?> queryAll() async{
    Database? db = await instance.notedatabase;
    return await db?.query(_tableName);
  }

  static Future<int?> update(Note note) async{
    Database? db = await instance.notedatabase;
    return await db?.update(_tableName, note.toMap(), where: '$columnId = ?', whereArgs: [note.id]);
  }

  static Future<int?> delete(Note note) async{
    Database? db = await instance.notedatabase;
    return await db?.delete(_tableName, where: '$columnId = ?', whereArgs: [note.id]);
  }
}

Please I am new to flutter. Can anyone help me out?

Full preoject(not completed): https://github.com/SayanBanerjee09082002/Daily_Utility

2

Answers


  1. _initializeNoteDatabase() this is a private method. If you come from a Java background then this method is the same as saying:

    private void initializeNoteDatabase() {}
    

    In dart, if you want to represent a private variables/methods then you use an underscore at the beginning. These private variables/methods can only be accessed in the same dart file. In your case _initializeNoteDatabase() is declared in the database helper file while you are trying to access it in the note list file, that’s not possible. To be able to access it, just remove the underscore at the beginning, making it a public method.

    Login or Signup to reply.
  2. Peter is correct, However your second problem is that you are calling a non-static method on the class itself, and not an instance of it.
    You have two options, either:

    static Future<Database?> initializeNoteDatabase() async {}
    final Future<Database> dbFuture = NoteDatabaseHelper.initializeNoteDatabase();
    

    Or:

    Future<Database?> initializeNoteDatabase() async {}
    final Future<Database> dbFuture = NoteDatabaseHelper().initializeNoteDatabase();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search