Aju
Aju

Reputation: 61

Flutter : how to check connectivity using firestore

How to check connectivity and show images only when there is an internet connection else show circular progress indicator. In my following code indicator is just appearing only for a second even when there is not an internet connection.

class About extends StatelessWidget {   
    List<Image> _listOfImages = <Image>[];
    @override
    Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Scaffold(    
      body:     ListView(
        scrollDirection: Axis.vertical,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.only(
                left: SizeConfig.safeBlockHorizontal * 5,
                top: SizeConfig.safeBlockHorizontal * 5,
                right: SizeConfig.safeBlockHorizontal * 5),
            child: Material(
              borderRadius: BorderRadius.circular(24.0),
              child: SizedBox(
                width: SizeConfig.safeBlockHorizontal * 80,
                height: SizeConfig.safeBlockHorizontal * 80,
                
                child: StreamBuilder<QuerySnapshot>(
                stream: Firestore.instance.collection('About').snapshots(),
   
                builder: (context, snapshot) {
                  if (snapshot.data.documents.length == 0){
                    return Center(
                        child: 
                                CircularProgressIndicator()
                      );                   
                  }
                   else {
                   return ListView.builder(
                      
                        itemCount: snapshot.data.documents.length,
                        itemBuilder: (BuildContext context, int index) {
                          _listOfImages = [];

Upvotes: 1

Views: 2256

Answers (1)

Alok
Alok

Reputation: 8998

Check this package out:

It is a great package/library which does this job for you. To make use of that, just import and do like this the below example:

      Future<bool> check() async {
        var connResult = await (Connectivity().checkConnectivity());
        if (connResult == ConnectivityResult.mobile) {
          return true;
        } else if (connResult == ConnectivityResult.wifi) {
          return true;
        }
        return false;
      }

Note that on Android, this does not guarantee connection to Internet. For instance, the app might have wifi access but it might be a VPN or a hotel WiFi with no access.

Alternate Usage

The main idea is to check whether the link can be opened or not, we're using google.com in this case. You can then, return your boolean to do your stuffs based upon that.

import 'dart:io'

bool isConnected = false;
// use try-catch to do this operation, so that to get the control over this 
// operation better
try{
  final result = await InternetAddress.lookup('google.com');
  if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
    // do the operation for connected, or change the bool to True for connected
    setState(() => isConnected = true);
  }
} on SocketException catch (_) {
   setState(() => isConnected = false);
}

To know more the InternetAddress, follow this: InternetAddress Class.

Upvotes: 1

Related Questions