0

I'm unable to get Supabase queries working with flutter. Here is an example of the code:

child: FutureBuilder<PostgrestResponse<dynamic>>(
   future: supabase.from('products').select().execute(),
      builder: (context, snapshot) {
         if (snapshot.connectionState != ConnectionState.active) {
            return const Center(
               child: SizedBox(
                  child: CircularProgressIndicator(),
                      width: 60,
                      height: 60,
                  ),
               );
            } else {
               return GridView.builder(
                  gridDelegate:
                      SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: gridColumns,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10,
                      ),
                      itemCount: snapshot.data!.length, // this line is causing an error
                      ...

I have no idea how to turn the length of the snapshot. It is was to turn this to streambuilder it would work. So really all i'm after is a basic example of using a futurebuilder with supabase.

Jake Anderson
  • 309
  • 1
  • 4
  • 18

2 Answers2

0

You can use if (snapshot.hasData) instead of checking snapshots connection state. Because it would be not including any data and connection state is not active.

So you can change your code as;

child: FutureBuilder<PostgrestResponse<dynamic>>(
   future: supabase.from('products').select().execute(),
      builder: (context, snapshot) {
         if (!snapshot.hasData) {
            return const Center(
               child: SizedBox(
                  child: CircularProgressIndicator(),
                      width: 60,
                      height: 60,
                  ),
               );
            } else {
               return GridView.builder(
                  gridDelegate:
                      SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: gridColumns,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10,
                      ),
                      itemCount: snapshot.data!.length, // this line is causing an error
                      ...
Mehmet Ali Bayram
  • 7,222
  • 2
  • 22
  • 27
0

While debugging the code, the response is coming as snapshot.data.data

enter image description here

So, the count value can be obtained by

itemCount: snapshot.data.data?.length,