skip to Main Content

I am trying to access a foreign-key field in a Supabase database using Flutter. None of the table rows has a null value when I view them on the Supabase dashboard, however, when I run my app in the simulator, I am getting an exception on line 53 (title: Text(data[‘payee(name)’]),) and I cannot figure out why. Examining the variable in the sidebar shows

  • data = Map (4 items)
  • 0 = "id" -> 2
  • 1 = "date" -> "2019-05-01"
  • 2 = "amount" -> 1290
  • 3 = "payee_id" -> Map (1 item)
  • key = "payee_id"
  • value = Map (1 item)
  • 0 = "name" -> "Goosehead Insurance"
  • index = 0

The code for my view follows:

class HomePage extends StatelessWidget {
  HomePage({super.key});
  final SupabaseClient supabase = Supabase.instance.client;

  Future<List> readData() async {
    final result = await supabase.from('transactions').select('''
    id,
    date,
    amount,
    payee_id (name)
    ''');
    return result;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Supabase Course'),
        actions: [
          IconButton(
            onPressed: () async {
              await supabase.auth.signOut();
            },
            icon: const Icon(Icons.logout),
          )
        ],
      ),
      body: FutureBuilder(
        future: readData(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasError) {
            return Center(
              child: Text(snapshot.error.toString()),
            );
          }

          if (snapshot.hasData) {
            if (snapshot.data.length == 0) {
              return const Center(
                child: Text("No data available.n Create new data"),
              );
            }
            return ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                var data = snapshot.data[index];
                return ListTile(
                  title: Text(data['payee(name)']), // <---error here!
                  trailing: IconButton(
                    onPressed: () {
                      Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) =>
                                  EditPage(data['id'].toString(), data['id'])));
                    },
                    icon: const Icon(Icons.chevron_right_outlined),
                  ),
                );
              },
            );
          }

          return const Center(
            child: CircularProgressIndicator(),
          );
        },
      ),
    );
  }
}

The error I get on line 53 is "_TypeError (type ‘Null’ is not a subtype of type ‘String’)" but, as I said, there’s no null values in the table, and the variable inspector doesn’t show any null values. I cannot figure out what the problem is.

2

Answers


  1. I don’t see a key of payee(name) in your data. Just payee_id.

    Login or Signup to reply.
  2. when you use data['payee(name)'] it will define data type as dynamic in case it can’t find value it that key and will return null (in this case your key is "payee(name)") in this case you can define type for value that return from future like this

    FutureBuilder<File>( // change File to type you want ex. model String etc.
      future: // your future must return value File like <File>,
      builder: (context, snapshot) {
     // return widget here
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search