【问题标题】:The function I wrote to fetch data in the database returns empty. There is data in the database. In flutter我编写的用于在数据库中获取数据的函数返回空。数据库中有数据。扑通扑通
【发布时间】:2021-05-23 10:58:15
【问题描述】:

======== 小部件库捕获的异常=================================== ====================== 在构建 Notlar(dirty, state: _NotlarState#20db1) 时引发了以下 NoSuchMethodError: 在 null 上调用了方法“notListesiniGetir”。 接收方:空 尝试调用:notListesiniGetir()

此处为照片 3 代码:

[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]

错误代码:======== 小部件库捕获的异常=============================== ========================= 在构建 Notlar(dirty, state: _NotlarState#20db1) 时引发了以下 NoSuchMethodError: 在 null 上调用了方法“notListesiniGetir”。 接收方:空 尝试调用:notListesiniGetir()

相关的导致错误的小部件是: Notlar 文件:///C:/Flutter%20calismalari/not_sepeti/lib/main.dart:63:27 抛出异常时,这是堆栈: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 _NotlarState.build (包:not_sepiti/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

【问题讨论】:

  • 我猜你不是在等待未来在某个地方完成。
  • 我是这个领域的新手。 :(

标签: firebase flutter dart future sqflite


【解决方案1】:

问题不在于您的代码,而在于您的屏幕截图(您应该将其作为代码 sn-p 添加到您的问题中)。

你必须等待条件snapshot.hasData

改变

If (snapshot.connectionState == ConnectionState.done)

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

还有

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

更重要的是在你的初始化状态中删除DatabaseHelper

DatabaseHelper databaseHelper = DatabaSeHelper();

databaseHelper = DatabaseHelper();

它会工作的。

【讨论】:

  • 你能打印 snapshot.data 并评论输出吗?
  • 还在 buildcontext 开头打印 databaseHelper ,这可能是原因。
  • lib/main.dart:161:73:错误:无法将“List”类型的值分配给“bool”类型的变量。 - “列表”来自“飞镖:核心”。 - “不是”来自“package:not_sepeti/models/notlar.dart”(“lib/models/notlar.dart”)。 if(snapshot.connectionState == ConnectionState.done && snapshot.data) {
  • 删除初始化状态中的 DatabaseHelper 关键字
  • 这是snapshot.hasData 而不是snapshot.data
猜你喜欢
  • 1970-01-01
  • 2021-12-06
  • 2022-01-22
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 2011-07-21
  • 2021-02-20
  • 1970-01-01
相关资源
最近更新 更多