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.

2 Answers 2

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
                      ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Mehmet, ok made that change but still unsure how you would output an itemCount for the builder?
Can you show inside your PostgrestResponse class, probably you need to get length of some property inside it
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,

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.