Sermed mayi
Sermed mayi

Reputation: 757

Flutter : Sqflite get error on future builder with snapshot.data.length

I have an app using sqflite ,I have this get data function in DataBaseHelper class:

getMatchs() async {
    final _db = await db;
    var result = await _db!.query('mathcTable');
    return result;
  }

in getX controller I have this method to get data :

get matchs => getMatchs();
  getMatchs() async {
    var list = await DataBaseHelper.instance.getMatchs();
    return list;
  }

on view page I want to use the list returned from controller :

final GameController _game = Get.put(GameController());

then use future builder:

body: FutureBuilder(
          future: _game.matchs,
          builder: (context, snapshot) {
            var list = snapshot.data

            ;
            if (snapshot.hasData && snapshot.data != null) {
              return ListView.builder(
                  itemCount: list!.length,
                  itemBuilder: (context, index) {
                    return Text('p');
                  });
            } else {
              return Text('waiting data');
            }
          })

this page give error on list.length as shown :

enter image description here

Edit when i print snapshot.data i get this :

[
{id: 1,
 playerOne: ,
 playerTwo: ,
 setCounts: 3,
 gameCounts: 6,
 firstPlayerserve: 1,
 isSuperTieBreake: 0},
 {id: 2,
 playerOne: ,
 playerTwo: ,
 setCounts: 3,
 gameCounts: 6,
 firstPlayerserve: 1,
 isSuperTieBreake: 0}
]

Upvotes: 0

Views: 411

Answers (1)

Benyamin
Benyamin

Reputation: 1154

Just add AsyncSnapshot before the snapshot passed in the list view for dart to recognize it. like this:

body: FutureBuilder(
          future: _game.matchs,
          builder: (context, AsyncSnapshot snapshot) {
            var list = snapshot.data

            ;
            if (snapshot.hasData && snapshot.data != null) {
              return ListView.builder(
                  itemCount: list!.length,
                  itemBuilder: (context, index) {
                    return Text('p');
                  });
            } else {
              return Text('waiting data');
            }
          })

Upvotes: 1

Related Questions