To check for internet connection i am using data_connection_checker package but it seems that I am not getting how to use it. I am using following code to check internet connection by calling in initstate
Widget _showDialog(String err,String content ,BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(err),
content: new Text(content),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.of(context).pop();
setState(() {
});
},
child: new Text("Try Again"))
]
);
}
);
}
Future checkConnection(BuildContext context) async{
listener = DataConnectionChecker().onStatusChange.listen((status) {
switch (status){
case DataConnectionStatus.connected:
return "DataConnectionState.connected";
break;
case DataConnectionStatus.disconnected:
InternetStatus = "You are disconnected to the Internet. ";
contentmessage = "Please check your internet connection";
_showDialog(InternetStatus,contentmessage,context);
break;
}
});
return await DataConnectionChecker().connectionStatus;
}
But I am also trying to check for internet connection in FutureBuilder widget too.I made following function to call to check
Future<bool> internetChecker(BuildContext context) async{
if( await DataConnectionChecker().hasConnection){
return true;
// Future<bool>.value(true);
}
else{
return false;
// Future<bool>.value(false);
}
}
But DataConnectionChecker().hasConnection is showing error until i use await before it like above.
FututreBuilder:
builder:(BuildContext context, AsyncSnapshot snapshot) {
if(internetChecker(context) == true){
if(snapshot.hasData){
if(snapshot.data==null){
return _showDialog('Unexpected Error', 'Data was null', context);
}
else{
return SafeArea(...);
}
}
else{
return Center(child: Image.asset("assets/images/369.gif",fit: BoxFit.fill,));
}
}
else{
return _showDialog('No Internet Connection', 'Check your Internet Connection and Try Again!!', context);
}
}
),
}
I am trying all of this in builder of FutureBuilder. Now, in if when i write 'internetChecker(context) == true' it shows me this !!!!!!!!!!!!!!!!
Equality operator '==' invocation with references of unrelated types.
!!!!!!!!!!!!!!
Edit: Moreover in internetchecker function during debugging i noticed it just jumps the if else condition and doesnot return any thing neither true nor false. I am not getting why.
Please tell me how to solve it or any other efficient way to do it.