0

I want to retrive data from table in db and display it into GridView in Flutter. At the moment I display it in a simple List but I can't display it in GridView. Here is my code

return Scaffold(
  appBar: AppBar(
    title: Text("Diario di bordo"),
  ),
  /*body: Center(
    child: Text('Nessun diario presente'),
  ),*/
    body: FutureBuilder<List>(
      future: dbHelper.queryAllRows(),
      initialData: List(),
      builder: (context, snapshot) {
        return snapshot.hasData
            ? ListView.builder(
          itemCount: snapshot.data.length,
          itemBuilder: (_, int position) {
            final item = snapshot.data[position];
            //get your item data here ...
            return Card(
              color: RandomColor().randomColor(),
              child: ListTile(
                title: Text(
                    snapshot.data[position].row[1]),
              ),
            );
          },
        )
            : Center(
          child: CircularProgressIndicator(),
        );
      },
    ),
...

I searched on documentation and I see this piece of code

GridView.count(
// Create a grid with 2 columns. If you change the scrollDirection to
// horizontal, this produces 2 rows.
crossAxisCount: 2,
// Generate 100 widgets that display their index in the List.
children: List.generate(100, (index) {
return Center(
  child: Text(
    'Item $index',
    style: Theme.of(context).textTheme.headline,
  ),
);
}),
);

I don't understand how retrive data from mytable in db and show it in GridView.

1 Answer 1

2

You can use the GridView.Builder

return snapshot.hasData? 
       GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2),
          itemCount: snapshot.data.length,
          itemBuilder: (BuildContext context, int index) {
            final item = snapshot.data[index];
            //get your item data here ...
            return Card(
              color: RandomColor().randomColor(),
              child: ListTile(
                 title: Text(
                    snapshot.data[index].row[1]),
              ),
            );
          },
        )
      : Center(
          child: CircularProgressIndicator(),
        );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.