从数据库中获取数据无法正常运行

时间:2019-06-24 12:17:37

标签: flutter dart

我在我的项目中使用scoped_model&sqflite,我试图从数据库中获取数据,并且它返回记录。我的代码段如下所示,

class ScopeManager extends Model {
  List<Book> _bookList = [];
  Database _myDb;
  Directory tempDir;
  String tempPath;

  ScopeManager() {
    createDB();
  }

  createDB() async {
    this.tempDir = await getTemporaryDirectory();
    this.tempPath = tempDir.path + "/mybooklibrary.db";
    // await this.deleteDB();

    await openDatabase(this.tempPath, version: 1, onOpen: (Database db) {
      this._myDb= db;
      print("--------------DB--------------OPEN DB");
      this.createTable();
    }, onCreate: (Database db, int version) async {
      this._myDb= db;
      print("--------------DB--------------DB CREATED");
    });
  }

  createTable() async {
    try {
      var qry = "CREATE TABLE IF NOT EXISTS " +
          tableWishList +
          " ( " +
          columnId +
          " INTEGER PRIMARY KEY autoincrement," +
          columnMedium +
          " TEXT," +
          columnGrade +
          " TEXT," +
          columnSubject +
          " TEXT," +
          columnChapter +
          " TEXT," +
          columnBookUrl +
          " TEXT," +
          columnAddedDate +
          " TEXT," +
          columnLastViewedAt +
          " TEXT)";
      await this._myDb.execute(qry);

      var _flag = storage.getItem("isFirst");
      print(_flag);

      if (_flag == "true") {
        this.fetchLibraryList();
      } else {
        // this.InsertInLocal();
      }
    } catch (e) {
      print(e);
    }
  }

  fetchLibraryList() async {
    try {
      // Get the records
      print("--------------DB--------------Library");
      _bookList = [];
      List<Map> list =
          await this._myDb.rawQuery('SELECT * FROM  ' + tableWishList);
      print(
          "--------------DB--------------Library len ${list.length.toString()}");
      list.map((dd) {
        Book bookItem = new Book();
        bookItem.id = dd[columnId];
        bookItem.medium = dd[columnMedium];
        bookItem.grade = dd[columnGrade];
        bookItem.subject = dd[columnSubject];
        bookItem.chapter = dd[columnChapter];
        bookItem.url = dd[columnBookUrl];
        bookItem.addedDate = dd[columnAddedDate];
        bookItem.lastViewdDate = dd[columnLastViewedAt];
        _bookList.add(bookItem);
      }).toList();
      notifyListeners();
    } catch (e) {
      print(e);
    }
  }

  List<Book> get libraryListing {
    return _bookList;
  }
}

书籍模型是

class Book {
  int id;
  String medium;
  String grade;
  String subject;
  String chapter;
  String url;
  String addedDate;
  String lastViewdDate;

  Book({
    this.medium,
    this.grade,
    this.subject,
    this.chapter,
    this.url,
    this.addedDate,
    this.lastViewdDate,
  });
}

在我的构建方法中,我使用的是scopemodel,

ScopedModelDescendant<ScopeManager>(
        builder: (context, child, model) {

          favBooksList = model.libraryListing;
          print('--------------DEBUG--------------' +
              favBooksList.length.toString());
          return favBooksList.length != 0
              ? ListView.builder(
                  itemCount: favBooksList.length,
                  itemBuilder: (BuildContext ctxt, int index) {
                    return wishItem(
                      favBooksList[index].chapter,
                      favBooksList[index].url,
                      favBooksList[index].medium,
                      favBooksList[index].grade,
                      favBooksList[index].subject,
                      favBooksList[index].addedDate,
                    );
                  },
                )
              : Center(
                  child: Container(
                    padding: EdgeInsets.all(50.0),
                    child: Text(
                      'Your book list is Empty!',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 25.0,
                        fontWeight: FontWeight.w400,
                        color: Colors.lightBlue,
                      ),
                      maxLines: 2,
                    ),
                  ),
                );
        },
      ),

实际上'favBooksList.length'不等于0,但返回0。

我该如何解决?我错过了什么吗?帮帮我

0 个答案:

没有答案