Search code examples
firebaseflutterdartfuturesqflite

The function I wrote to fetch data in the database returns empty. There is data in the database. In flutter


======== Exception caught by widgets library ======================================================= The following NoSuchMethodError was thrown building Notlar(dirty, state: _NotlarState#20db1): The method 'notListesiniGetir' was called on null. Receiver: null Tried calling: notListesiniGetir()

Photo-3 code here:

[Future<int> kategoriSil(int kategoriID) async {
    var db= await _getDatabase();
    var sonuc= await db.delete("kategori",  where: 'kategoriID= ?', whereArgs: \[kategoriID\]);
    return sonuc;
  }

  Future<List<Map<String, dynamic>>> notlariGetir() async {
    var db = await _getDatabase();
    var sonuc= await db.rawQuery('select * from "not" inner join kategori on kategori.kategorID = "not".kategoriID;');
    return sonuc;
  }
  Future<List<Not>> notListesiniGetir() async{
    var notlarMapListesi = await notlariGetir();
    var notListesi = List<Not>();
    for(Map map in notlarMapListesi){
      notListesi.add(Not.fromMap(map));

    }
    return notListesi;

  }][1]

error code:======== Exception caught by widgets library ======================================================= The following NoSuchMethodError was thrown building Notlar(dirty, state: _NotlarState#20db1): The method 'notListesiniGetir' was called on null. Receiver: null Tried calling: notListesiniGetir()

The relevant error-causing widget was: Notlar file:///C:/Flutter%20calismalari/not_sepeti/lib/main.dart:63:27 When the exception was thrown, this was the stack: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 _NotlarState.build (package:not_sepeti/main.dart:166:30) #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4802:27) #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4685:15) #4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4857:11) ...

enter image description hereBwg.png


Solution

  • The problem is not in your code but in your screenshot (that you should add to your question as a code snippet).

    You have to await the condition snapshot.hasData

    Change from

    If (snapshot.connectionState == ConnectionState.done)
    

    To

    If (snapshot.connectionState == ConnectionState.done && snapshot.hasData)
    

    Also

    var notListesi = List<Not>()
    List<Not> notListesi = <Not>[ ]; // better
    
    

    And more importantly remove DatabaseHelper in your init state:

    DatabaseHelper databaseHelper = DatabaSeHelper();
    

    To

    databaseHelper = DatabaseHelper();
    

    it will be working.