1

I am trying to print out multiple data from Supabase using flutter but it just isn't working

getResponse(String uid) async {
    await client
        .from("privChatRoom")
        .select()
        .eq('userID', uid)
        .single()
        .execute()
        .then((value) {
      if (value.error == null) {
        print('value.data: ${value.data}');
      } else {
        print(value.error.message.toString());
        print(value.error.hint.toString());
      }
    });
  }

This is the error i get whenever i run this function

I/flutter ( 2326): JSON object requested, multiple (or no) rows returned
I/flutter ( 2326): null

I have multiple row with the same uid that i want to get please how do i get it

EDIT: Found the answer i just had to remove .single()

dshukertjr
  • 15,244
  • 11
  • 57
  • 94
Brightcode
  • 660
  • 9
  • 27

3 Answers3

3

Just here to clarify.

If you use single() filter, your result has to return 1 and only 1 result, and otherwise it will fail. If you want multiple return values, you can just get rid of the single() like this:

getResponse(String uid) async {
    await client
        .from("privChatRoom")
        .select()
        .eq('userID', uid)
        .execute()
        .then((value) {
      if (value.error == null) {
        print('value.data: ${value.data}');
      } else {
        print(value.error.message.toString());
        print(value.error.hint.toString());
      }
    });
  }
dshukertjr
  • 15,244
  • 11
  • 57
  • 94
0

If are expecting a result to not have a single response, call the query without .single(). You should get an empty result then.

Or maybe the uid value doesn't exit in your database!

Try to replace .single() with .limit(1),

Don't forget this function returns an array! So you have to get the data like this value.data[0], If no data exist then the value.data will be an empty array [].

If you expect to get 0 rows though you might want to use .maybeSingle() instead.

Read this: https://postgrest.org/en/stable/api.html#singular-or-plural

zakaria chahboun
  • 481
  • 6
  • 13
0

I'm not a flutter user, but using .single() should work the same way as in the Javascript client.

.limit(1) <= returns a single record as an array [{id:123,firstname:'Harry',lastname:'Styles'}]

.single() <= returns a single record as an object {id:123,firstname:'Harry',lastname:'Styles'}

Mark Burggraf
  • 456
  • 2
  • 6
  • 1
    Please make sure your answers are unique. Your answer seems very similar to one of the previous answers. I suggest you read [answer] – Rojo Oct 14 '21 at 17:07