Ir para conteúdo
Fluttership
  • Cadastre-se
Alex Castro

Checar conexão com internet

Recommended Posts

O future builder retonar o estado da conexão, mas e se você tiver com os dados móveis ligado, mas sem créditos???

 

E se quiser retornar dados de acordo com o wifi e dados móveis para economizar banda?

 

Então, use connectivity:

 

connectivity: ^0.4.2

 

Mão na massa:

 

// Importing Flutter Package

import 'package:connectivity/connectivity.dart';

 

var connectivityResult = await (Connectivity().checkConnectivity());

if (connectivityResult == ConnectivityResult.mobile) {

  print("Connected to Mobile Network");

} else if (connectivityResult == ConnectivityResult.wifi) {

  print("Connected to WiFi");

} else {

  print("Unable to connect. Please Check Internet Connection");

}

 

Ou você pode ser notificado caso o estado da conexão for alterado

 

 

// Importing Flutter Package

import 'package:connectivity/connectivity.dart';

 

 

initState() {

  subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {

    print("Connection Status has Changed");

  })

}

 

dispose() {

  subscription.cancel();

}

 

É isso aí galera!! Bom código 

 

Fonte: https://www.codemeals.com/flutter/network-connectivity-flutter/

Compartilhar este post


Link para o post
Compartilhar em outros sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Visitante
Responder

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Criar Novo...