0

Help is deeply appreciated as I have been trying to solve this problem for a couple of days now but still unable to figure out the problem.

All the code below use to work perfectly, until recently, the list stops updating like it use to until I hot restart it.

The below two functions are getting my map list and my note list respectively

Future<List<Map<String, dynamic>>> getNoteListMap() async {
    Database db = await this.database;
    var result = await db.query(noteTable);
    return result;
  }


Future<List<Note>> getNoteList() async {
    var noteMapList = await getNoteListMap();
    int count = noteMapList.length;
    //list of notes, each note consist of their own independent variables
    List<Note> noteList = new List<Note>();
    for (int i = 0; i < count; i++) {
      noteList.add(Note.fromMapObject(noteMapList[i]));
    }
    return noteList;
  }

This is my main function , which creates a list using FutureBuilder. However the list doesnt get updates, and only does after I hot restart everytime.

FutureBuilder<List<Note>>(
  future: _databaseHelper.getNoteList(),
  builder: (BuildContext context, AsyncSnapshot<List<Note>> snapshot) {
    if (snapshot.hasData) {
      return ListView.builder(
        physics: TargetPlatform.iOS!=null? BouncingScrollPhysics() : ClampingScrollPhysics(),
        itemCount: snapshot.data.length,
        itemBuilder: (BuildContext context, int position) {
          Note note = snapshot.data[position];
          return Card(
            color: Colors.white,
            elevation: 2.0,
            child: new ListTile(
              title: new Text(note.title?? ''),
              subtitle: new Text(note.bodyText ?? ''),
              onTap: () => _navigateToTaskByDatePage(date),
            ),
          );
        }
      );
    } else if (snapshot.hasError) {
      return Container(
        child: Center(
          child: new Text('Error: ${snapshot.error}'),
        ),
      );
    } else {
      return Container(
        width: 0,
        height: 0,
      );
    }
  },
),
J. S.
  • 8,905
  • 2
  • 34
  • 44
Wei Jun
  • 597
  • 2
  • 6
  • 10
  • There doesn't seem to be anything wrong with your code. It's difficult to help you debug as we don't have access to your database. Have you tried implementing the checks on the [AsyncSnapshot](https://api.flutter.dev/flutter/widgets/AsyncSnapshot-class.html)'s [ConnectionState](https://api.flutter.dev/flutter/widgets/ConnectionState-class.html) to see what's going on? – J. S. Feb 02 '20 at 11:45
  • Will look into that, thanks for the advice. – Wei Jun Feb 02 '20 at 11:47
  • Did you try this using a `StreamBuilder` instead of `FutureBuilder`? [Here's](https://stackoverflow.com/questions/50844519/flutter-streambuilder-vs-futurebuilder) a SO question and answers on the difference between the two. – JJuice Feb 02 '20 at 12:02
  • try calling `setState()` method after you called the function, it will change the app state. – Vicky Salunkhe Feb 02 '20 at 12:03

2 Answers2

0

How about this?

class ... {
  Future<List<Note>> futureNoteList;

  @override
  void initState() {
    super.initState();
    futureNoteList = _databaseHelper.getNoteList();
  }

  FutureBuilder<List<Note>>(
    future: futureNoteList,
    ...
  ),

  void update() async {
    val newList = await _databaseHelper.getNoteList();
    setState(() {
      futureNoteList = newList;
    });
  }
Yuu Woods
  • 1,303
  • 6
  • 17
0

Thanks for all the solutions provided to help solve the problem I faced! However, I managed to resolve my problem by doing flutter upgrade. Apparently, there's nothing wrong with my code, and it's just a flutter version issue.

Wei Jun
  • 597
  • 2
  • 6
  • 10