Search code examples
postgresqlflutterdartsupabase

How to fetch data from supabase in flutter?


This is now my database in supabase. I'm trying to fetch all the rows with post id 1. So I want to get potato, carrot, meat, water.

ingredient_id(pk) post_id(fk) order ingredient
1 1 1 potato
2 1 2 carrot
3 1 3 meat
4 1 4 water

But I don't know if I should use stream or select or something. When I use .select('ingredient').eq('post', 1), I don't know what to use next, not single.

fetchData() {
  final stream = Supabase.instance.client
  .from('post_ingredient')
  .stream(primaryKey: ['ingredient_id'])
  .order('order', ascending: true);

  return StreamBuilder<List<Map<String, dynamic>>>(
    stream: stream,
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return const Center(child: CircularProgressIndicator(),);
      }
      final table = snapshot.data!;

      return ListView.builder(
        itemCount: table.length,
        itemBuilder: (context, index) {
          return Text(table[index]['ingredient'].toString());
        },
      );
    }
  );
 }
@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: _appBar(),
    body: fetchData(),
  );
}

Please give me a solution or tips, idea.


Solution

  • I finally solved it in three days.

    final stream = Supabase.instance.client
      .from('post_ingredient')
      .stream(primaryKey: ['ingredient_id'])
      .inFilter('post_id', [1]) // add this
      .order('order', ascending: true);
    

    Too few sorce codes use flutter and superbase at the same time ㅠㅠ