Search
 
SCRIPT & CODE EXAMPLE
 

DART

getting internet connectivity in flutter with getx

import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class NetworkController extends GetxController {
  static NetworkController to = Get.find();

  //this variable 0 = No Internet, 1 = connected to WIFI ,2 = connected to Mobile Data.
  //Instance of Flutter Connectivity

  int connectionType = 0;
  final Connectivity _connectivity = Connectivity();
  //Stream to keep listening to network change state
  late StreamSubscription _streamSubscription;
  @override
  void onInit() {
    super.onInit();
    _getConnectionType();
    _streamSubscription =
        _connectivity.onConnectivityChanged.listen(_updateState);
  }

  // a method to get which connection result, if you we connected to internet or no if yes then which network
  Future<void> _getConnectionType() async {
    ConnectivityResult? connectivityResult;
    try {
      connectivityResult = await (_connectivity.checkConnectivity());
    } on PlatformException catch (e) {
      print(e);
    }
    return _updateState(connectivityResult!);
  }

  // state update, of network, if you are connected to WIFI connectionType will get set to 1,
  // and update the state to the consumer of that variable.
  _updateState(ConnectivityResult result) {
    switch (result) {
      case ConnectivityResult.wifi:
        connectionType = 1;
        update();
        break;
      case ConnectivityResult.mobile:
        connectionType = 2;
        update();
        break;
      case ConnectivityResult.none:
        connectionType = 0;
        update();
        break;
      default:
        Get.snackbar('Network Error', 'Failed to get Network Status');
        break;
    }
  }

  @override
  void onClose() {
    //stop listening to network state when app is closed
    _streamSubscription.cancel();
    super.onClose();
  }
}
Comment

getting internet connectivity in flutter with getx

import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class NetworkController extends GetxController {
  static NetworkController to = Get.find();

  //this variable 0 = No Internet, 1 = connected to WIFI ,2 = connected to Mobile Data.
  //Instance of Flutter Connectivity

  int connectionType = 0;
  final Connectivity _connectivity = Connectivity();
  //Stream to keep listening to network change state
  late StreamSubscription _streamSubscription;
  @override
  void onInit() {
    super.onInit();
    _getConnectionType();
    _streamSubscription =
        _connectivity.onConnectivityChanged.listen(_updateState);
  }

  // a method to get which connection result, if you we connected to internet or no if yes then which network
  Future<void> _getConnectionType() async {
    ConnectivityResult? connectivityResult;
    try {
      connectivityResult = await (_connectivity.checkConnectivity());
    } on PlatformException catch (e) {
      print(e);
    }
    return _updateState(connectivityResult!);
  }

  // state update, of network, if you are connected to WIFI connectionType will get set to 1,
  // and update the state to the consumer of that variable.
  _updateState(ConnectivityResult result) {
    switch (result) {
      case ConnectivityResult.wifi:
        connectionType = 1;
        update();
        break;
      case ConnectivityResult.mobile:
        connectionType = 2;
        update();
        break;
      case ConnectivityResult.none:
        connectionType = 0;
        update();
        break;
      default:
        Get.snackbar('Network Error', 'Failed to get Network Status');
        break;
    }
  }

  @override
  void onClose() {
    //stop listening to network state when app is closed
    _streamSubscription.cancel();
    super.onClose();
  }
}
Comment

PREVIOUS NEXT
Code Example
Dart :: convert future to stream using stream.fromfuture dart 
Dart :: list in dart 
Dart :: how to sort and order a list by date in flutter 
Dart :: flutter container image overlay 
Dart :: flutter ios status bar is dark 
Dart :: phone authentication firebase flutter 
Dart :: flutter extend two classes 
Dart :: how to get isoCode based on location in flutter 
Dart :: Invalid argument(s): join(null, "bin", "cache", "dart-sdk"): part 0 was null, but part 1 was not. 
Dart :: how to put two trailing icons in list tile flutter 
Dart :: flutter delete directory 
Dart :: create a int list dart 
Dart :: onpressed pass context flutter 
Dart :: dart regex,regex dart 
Dart :: flutter pageview show next page 
Dart :: dart while loop 
Dart :: flutter text in row not wrapping 
Dart :: flutter button playing sound 
Dart :: how to replace string character in dart 
Dart :: flutter map 
Dart :: how to effect container radius to children flutter 
Dart :: flutter provider difference between Consumer<T and context.watch<T 
Dart :: flutter dollar sign interpolation 
Dart :: Flutter: How to point to localhost:8000 with the Dart http package in Flutter? 
Dart :: dart is operator 
Dart :: cascade notation 
Swift :: main thread swift 
Swift :: convert string to int swift 
Swift :: date formatter swift 
Swift :: swift set uiimage color 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =