kauemurakami / getx_pattern

Design pattern designed to standardize your projects with GetX on Flutter.

Home Page:https://kauemurakami.github.io/getx_pattern

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Where would I put the StreamSubscription?

xxxLucifeRxxx opened this issue · comments

Hi, I follow your architecture and have sorted out all the examples, I really like using it. But I have such a problem, I have a need to monitor the Internet connection during the entire life cycle of the application. And I do not know where I could put the Stream Subscription for this tracking. Since I need to keep track of this constantly, I thought I could do it in my authService extends GetxService, since it is not deleted from memory and lives throughout the entire cycle.

And I would not like to perform this check with every network request I have, I would like to know about the network status always. I tried to do something similar, but I didn't get the desired result:

class AuthService extends GetxService{
  Future<AuthService> init() async{
    repository = AuthRepository(MyApi());
    _subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) async { 
      if (result == ConnectivityResult.none){
        isOfflineMode.value = false;
        Get.snackbar('No connect','There is no internet connection');
      }
      else if(result == ConnectivityResult.mobile){
        isOfflineMode.value = await InternetConnectionChecker().hasConnection;
        if(isOfflineMode.value == true){
          Get.snackbar('Нет соединения','There is no internet connection');
        }
      }
      else if(result == ConnectivityResult.wifi){
        isOfflineMode.value = await InternetConnectionChecker().hasConnection;
        if(isOfflineMode.value == true){
          Get.snackbar('Нет соединения','There is no internet connection');
        }
      }
    });
    return this;
  }

  late StreamSubscription<ConnectivityResult> _subscription;

  final isOfflineMode = false.obs;
}

I could certainly put it in controllers, but I think there will be a lot of boilerplate :)

Could you help me with this, how I could better organize the work of such functionality, I would be very grateful

Hi, I'm glad you're using and enjoying the architecture.
I had the same need a few days ago and I found the solution with a package

runApp(GetMaterialApp(
     onInit: (() {
       subscription = Connectivity()
           .onConnectivityChanged
           .listen((ConnectivityResult result) {
         if (result == ConnectivityResult.none) {
           Get.to(NotConnectionWidget());
         }
       });
     }),
     onDispose: () => subscription.cancel(),
     // debugShowMaterialGrid: true,
     theme: appTheme,
     ...

But if you want to do it manually, I still recommend using it from the start of the app, in the main itself.
You can organize it better by separating the methods into single files.

Hi, I'm glad you're using and enjoying the architecture. I had the same need a few days ago and I found the solution with a package

runApp(GetMaterialApp(
     onInit: (() {
       subscription = Connectivity()
           .onConnectivityChanged
           .listen((ConnectivityResult result) {
         if (result == ConnectivityResult.none) {
           Get.to(NotConnectionWidget());
         }
       });
     }),
     onDispose: () => subscription.cancel(),
     // debugShowMaterialGrid: true,
     theme: appTheme,
     ...

But if you want to do it manually, I still recommend using it from the start of the app, in the main itself. You can organize it better by separating the methods into single files.

Thank you, this is what I was looking for, I put my code in GetMaterialApp, and now there is feedback from StreamSubscription