flutterdata / flutter_data

Seamlessly manage persistent data in your Flutter apps
MIT License
410 stars 31 forks source link

Offline always in loading state #263

Closed aacevedocofrai closed 7 months ago

aacevedocofrai commented 8 months ago

Hello i tried, 1.5 and 1.6 the offline code you shared in the doc:

FYI I'm testing on web

    final initializerProvider = FutureProvider<void>((ref) async {
      // initialize FD
      await ref.container.read(repositoryInitializerProvider.future);

      // initialize other services

      // retry offline events
      final _sub = ref.listen(offlineRetryProvider, (_, __) {});

      // close offline retry subscription
      ref.onDispose(() {
        _sub.close();
      });
    });

but my app stay in loading state forever, if y remove it, it works with no problem

class UsersApp extends HookConsumerWidget {

  const UsersApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {

    final initializerProvider = FutureProvider<void>((ref) async {
      // initialize FD
      await ref.container.read(repositoryInitializerProvider.future);

      // initialize other services

      // retry offline events
      final _sub = ref.listen(offlineRetryProvider, (_, __) {});

      // close offline retry subscription
      ref.onDispose(() {
        _sub.close();
      });
    });

    // https://github.com/rrousselGit/riverpod/issues/875
    // final initializerProvider = FutureProvider<void>((ref) {
    //   log('hola');
    //   // initialize FD
    //   // ref.users.logLevel = 2;
    //   // await ref.container.refresh(repositoryInitializerProvider.future);
    //   ref.container.refresh(repositoryInitializerProvider);
    //   log('hola done');
    //     // retry offline events
    //   final _sub = ref.listen(offlineRetryProvider, (_, __) {});

    //   // close offline retry subscription
    //   ref.onDispose(() {
    //     _sub.close();
    //   });
    // });
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ref.watch(initializerProvider).when(
                error: (error, _) => Text(error.toString()),
                loading: () => const Text('Loading...'),
                data: (_) {
                  log('DATA');
                  // enable verbose
                  return const UsersScreen();
                },
              ),
        ),
      ),
      debugShowCheckedModeBanner: false,
    );
  }
}
aacevedocofrai commented 8 months ago

Update this way works:

import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_data/flutter_data.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:poc_flutter_data/main.data.dart';
import 'package:poc_flutter_data/src/user_feature/user_screen.dart';

class UsersApp extends HookConsumerWidget {

  const UsersApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {    
    // https://github.com/rrousselGit/riverpod/issues/875
    final initializerProvider = FutureProvider<void>((ref) {
      // initialize FD
      // await ref.container.refresh(repositoryInitializerProvider.future);
      ref.container.refresh(repositoryInitializerProvider);
        // retry offline events
      final _sub = ref.listen(offlineRetryProvider, (_, __) {});

      // close offline retry subscription
      ref.onDispose(() {
        _sub.close();
      });
    });
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ref.watch(initializerProvider).when(
                error: (error, _) => Text(error.toString()),
                loading: () => const Text('Loading...'),
                data: (_) {
                  log('DATA');
                  // enable verbose
                  return const UsersScreen();
                },
              ),
        ),
      ),
      debugShowCheckedModeBanner: false,
    );
  }
}
frank06 commented 7 months ago

Thank you for sharing the working version