Search code examples
androidfluttersupabase

flutter_supabase can't get snapshot.data in ANDROID RELEASE MODE


I cannot connect to a database connection in an application using flutter_supabase. snapshot.hasData stays false all the time and does not change.

This occurs only in Android release mode; it does not occur in Android debug mode, web debug or release mode.

I have also tried minifyEnabled false and shrinkResources false at android/app/build.gradle, and Codemagic build, but no improvement.

Please advise.

The code below is for a TODO management application.

# pubspec.yaml
dependencies:
  flutter:
    sdk: flutter

  flutter_dotenv: ^5.1.0
  supabase_flutter: ^1.10.6
// main.dart
void main() async {
  await dotenv.load(fileName: ".env");
  await Supabase.initialize(
    // url: dotenv.get('URL'),
    // anonKey: dotenv.get('ANON_KEY'),
    url: 'MY_URL_VAL',
    anonKey: 'MY_ANON_KEY_VAL',
  );

  runApp(const MyApp());
}
// todos.dart
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

class Todos extends StatefulWidget {
  const Todos({Key? key}) : super(key: key);

  @override
  State<Todos> createState() => _TodosState();
}

class _TodosState extends State<Todos> {
  final _stream = Supabase.instance.client
      .from('todos')
      .stream(primaryKey: ['id'])
      .order('created_at', ascending: true);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Todos'),
      ),
      body: StreamBuilder<List<Map<String, dynamic>>>(
        stream: _stream,
        builder: (context, snapshot) {

          // !!! snapshot.hasData remains false and does not change   !!!
          // !!! Progress indicator is displayed and no data is shown !!!
          if (!snapshot.hasData) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }

          final todos = snapshot.data!;

          return ListView.builder(
            itemCount: todos.length,
            itemBuilder: (context, index) {
              return ListTile(
                title: Text(todos[index]['name']),
                subtitle: Text(todos[index]['created_at']),
                trailing: SizedBox(
                  width: 100,
                  child: Row(
                    children: [
                      IconButton(
                        onPressed: () => {},
                        icon: const Icon( Icons.delete ),
                      ),
                      IconButton(
                        onPressed: () => {},
                        icon: const Icon( Icons.done),
                      ),
                    ],
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}


Solution

  • Make sure you have Internet permission in AndroidManifest file.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.app">
        <uses-permission android:name="android.permission.INTERNET"/>
        ...
       <application ...