daZeus
daZeus

Reputation: 13

FutureBuilder returns no data but "Instance of Object"

i'm currently working on a app which basically works like a database. I want it to fetch data from my sqflite database and use it as text. For some reason I only get "Index of Object" instead of the correct value as text. This is my code in database_helper.dart:

  Future<Obj> getName(int id) async {
    final db = await database;
    var res = await  db.query(table, columns: [objName], where: "objId= ?", whereArgs: [id]);
    await print(res);         //returns right value
    return await res.isNotEmpty ? Fish.fromMap(res.first) : Null ;
  }
}

and this is my FutureBuilder

FutureBuilder(
            future: dbHelper.getName(1),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.hasData) {
                return ListTile(
                    leading: Icon(Icons.message),
                    title: Text('${snapshot.data}'),
                    subtitle: Text(""),
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => NextPage()),
                      );
                    }
                    );
            }
              return Container();
            },
          ),

I feel like I tried everything. Printing res directly from database_helper.dart gives the right result. I'd love some tipps! Thanks in advance!

Upvotes: 1

Views: 314

Answers (1)

Haroon Ashraf Awan
Haroon Ashraf Awan

Reputation: 1219

FutureBuilder is working fine and is returning data.You have one Fish object returned from your getName future. snapshot.data has returned one Fish object.To show any property you can use snapshot.data.propertyName of Fish Model.

Upvotes: 1

Related Questions