Network connection cubit

PHOTO EMBED

Tue Jan 09 2024 10:07:36 GMT+0000 (Coordinated Universal Time)

Saved by @Samuel1347 #flutter #dart

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';



part 'network_state.dart';

class NetworkCubit extends Cubit<NetworkState> {
  final NetworkInfo networkService;

  NetworkCubit({required this.networkService}) : super(NetworkInitial()) {
    listenConnection();
  }

  static bool _isOnline = false;

  bool get isOnline => _isOnline;

  listenConnection() async {
    networkService.connectionStream.listen((status) {

      if(status == InternetConnectionStatus.connected) {
        _isOnline = true;
        emit(NetworkConnectedState());
      }else {
        _isOnline = false;
        emit(NetworkLostState());
      }
    });
  }
}
content_copyCOPY

Need with network connection service