使用 data_connection_checker 包时使用不相关类型的引用调用相等运算符“==”

Gau*_*yal 8 connection future flutter flutter-futurebuilder

为了检查互联网连接,我正在使用 data_connection_checker 包,但似乎我不知道如何使用它。我正在使用以下代码通过调用 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;
  }

Run Code Online (Sandbox Code Playgroud)

但我也尝试在 FutureBuilder 小部件中检查互联网连接。我做了以下函数来调用来检查

Future<bool> internetChecker(BuildContext context) async{
    if( await DataConnectionChecker().hasConnection){
      return true;
        // Future<bool>.value(true);
    }
    else{
      return false;
        // Future<bool>.value(false);
    }
  }
Run Code Online (Sandbox Code Playgroud)

但是 DataConnectionChecker().hasConnection 显示错误,直到我像上面一样在它之前使用等待。

未来建造者:

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);
            }
          }
        ),
     }
Run Code Online (Sandbox Code Playgroud)

我正在 FutureBuilder 的构建器中尝试所有这些。现在,如果当我写“internetChecker(context) == true”时,它会向我显示这个!!!!!!!!!!!!!!!

Equality operator '==' invocation with references of unrelated types.

!!!!!!!!!!!!!!!

编辑:此外,在调试期间的 internetchecker 功能中,我注意到它只是跳过 if else 条件,并且不返回任何既非真也非假的东西。我不明白为什么。

请告诉我如何解决它或任何其他有效的方法来做到这一点。

小智 -3

您比较 2 个元素,但不是同一类型。Future <bool> 与 bool 不是同一类型。

尝试

internetChecker(context).then((value){
value == true ? ...
});
Run Code Online (Sandbox Code Playgroud)