mobileappdevhm19 / HINT-Reader

apps4students-team-3 created by GitHub Classroom
Apache License 2.0
0 stars 0 forks source link

Database with sqflite #45

Closed GiorgioBullo closed 5 years ago

GiorgioBullo commented 5 years ago

Dear Tobias,

I created a database in the branch feature/database. The database is very similar to that one you showed us in class, the main difference is that my class DBProvider create a static implementation of the database such that it's not possible to create more than one instance in the application. The DBProvider is in the file database.dart, while it is used in View/BookShelfView.dart.

THE PROBLEM: The problem is that when someone either add/delete an item or add all the items from the json, or delete all the items, then the scrollview in the screen is not updated (even if, I think, I use all the needed setState()). You can see that everything works fine, except from this, because, for instance, if you: -delete everything, -then you try to add a new item (you are redirect to a new page) -and you come back withouth adding anything; you see that now the page is updated. So it seems like the page is updated only if you go to another page and then you come back.

Hoping this will be clear, thanks in advance. Giorgio.

GiorgioBullo commented 5 years ago

Maybe I got the problem, please tell me if I'm right. The scrollview is in the verticalTable() (or horizontalTable()) in BookShelfView and when I use setState() outside of the verticalTable and so the scrollview is not updated because it is in a different item's tree.

The problem is solved using _getBooks() inside verticalTable. However, now, the console seems to be overloaded, because it continuously refresh the page.

RedTo commented 5 years ago

Hi Giorgio,

I didn't had a look at the source code now, so it is a little bit complicated to say anything. But I will show into this later this day.

Your right, that it could be possible, that setState is called on a different items tree, but that depends on how you build the desired page. Usually setState should be called for the page (Statefull / StatelessWidget integration).

Sorry that I can not tell you more at this point. I will respond as soon as possible.

RedTo commented 5 years ago

Okay, so I had a closer look and at the end I think it is a good solution.

RedTo commented 5 years ago

BookshelfView Line 45 make deleteAll return a Future for example, then you can write something like deleteAll().then((value) => {_getBooks()});

BookshelfView Line 64 do the same here: ).then((value) => {_getBooks()});

BookshelfView Line 100 '_getBooks(update: true);`

BookshelfView Line 117 ).then((value) => {_getBooks()});

BookshelfView Line 141 _getBooks(rebuild: false);

BookshelfView Line 164 _getBooks(rebuild: false);

BookshelfView Line 190 (whole method)

  Future<void> _getBooks({bool update = false, bool rebuild = true}) async {
    List<Book> books = new List<Book>(); //create a empty list
    if (update) {
      //if update is marked
      await DBProvider.db
          .updateBooks(context); //fetch data from the json and update database
    }
    books = await DBProvider.db.getAllBooks(); //load contacts from the database

    if (rebuild) {
      //call set state to change the displayed contacts
      setState(() {
        _books = books;
      });
    }
  }

I think you can delete those lines:

GiorgioBullo commented 5 years ago

Thank you very much, today i'll try it. Tomorrow, if it's ok, i'll be glad to discuss a little with you about this issue.

In BookshelfView line 120 - 126 I was trying to push and pop quickly a scaffold, just to see if it would have caused a rebuild hehehe then I forgot to delete it.

GiorgioBullo commented 5 years ago

Everything is fine now. Thanks again.