Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 扑动未来<;布尔>;vs布尔型_Flutter_Future - Fatal编程技术网

Flutter 扑动未来<;布尔>;vs布尔型

Flutter 扑动未来<;布尔>;vs布尔型,flutter,future,Flutter,Future,我的颤振项目有一个utility.dart文件和一个main.dart文件。我调用了main.dart文件中的函数,但它有问题。它总是显示“OK”,我认为问题在于实用程序类checkConnection()返回一个未来的bool类型 main.dart: if (Utility.checkConnection()==false) { Utility.showAlert(context, "internet needed"); } else { Utility.showAlert(cont

我的颤振项目有一个utility.dart文件和一个main.dart文件。我调用了main.dart文件中的函数,但它有问题。它总是显示“OK”,我认为问题在于实用程序类checkConnection()返回一个未来的bool类型

main.dart:

if (Utility.checkConnection()==false) {
  Utility.showAlert(context, "internet needed");
} else {
  Utility.showAlert(context, "OK");
} 

utility.dart:

import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'dart:async';

class Utility {


  static Future<bool> checkConnection() async{

    ConnectivityResult connectivityResult = await (new Connectivity().checkConnectivity());

    debugPrint(connectivityResult.toString());

    if ((connectivityResult == ConnectivityResult.mobile) || (connectivityResult == ConnectivityResult.wifi)){
      return true;
    } else {
      return false;
    }
  }

  static void showAlert(BuildContext context, String text) {
    var alert = new AlertDialog(
      content: Container(
        child: Row(
          children: <Widget>[Text(text)],
        ),
      ),
      actions: <Widget>[
        new FlatButton(
            onPressed: () => Navigator.pop(context),
            child: Text(
              "OK",
              style: TextStyle(color: Colors.blue),
            ))
      ],
    );

    showDialog(
        context: context,
        builder: (_) {
          return alert;
        });
  }
}
导入“包装:颤振/材料.省道”;
导入“package:connectivity/connectivity.dart”;
导入“dart:async”;
类效用{
静态未来checkConnection()异步{
ConnectivityResult ConnectivityResult=await(新建连接().checkConnectivity());
debugPrint(connectivityResult.toString());
if((connectivityResult==connectivityResult.mobile)| |(connectivityResult==connectivityResult.wifi)){
返回true;
}否则{
返回false;
}
}
静态void showAlert(BuildContext上下文、字符串文本){
var alert=新建警报对话框(
内容:容器(
孩子:排(
儿童:[文本(文本)],
),
),
行动:[
新扁平按钮(
onPressed:()=>Navigator.pop(上下文),
子:文本(
“好的”,
样式:TextStyle(颜色:Colors.blue),
))
],
);
显示对话框(
上下文:上下文,
建筑商:(){
返回警报;
});
}
}

您需要从
未来
中获取
bool
。使用can
然后阻塞
等待

带then块

_checkConnection() {
  Utiliy.checkConnection().then((connectionResult) {
    Utility.showAlert(context, connectionResult ? "OK": "internet needed");
  })
}
等待

_checkConnection() async {
 bool connectionResult = await Utiliy.checkConnection();
 Utility.showAlert(context, connectionResult ? "OK": "internet needed");
}

有关更多详细信息,请参阅。

在未来函数中,您必须返回未来结果,因此您需要更改以下结果的返回:

return true;
致:

返回Future.value(true);
因此,正确返回的完整函数是:

 static Future<bool> checkConnection() async{

    ConnectivityResult connectivityResult = await (new Connectivity().checkConnectivity());

    debugPrint(connectivityResult.toString());

    if ((connectivityResult == ConnectivityResult.mobile) || (connectivityResult == ConnectivityResult.wifi)){
      return Future<bool>.value(true);
    } else {
      return Future<bool>.value(false);
    }
  }
static Future checkConnection()异步{
ConnectivityResult ConnectivityResult=await(新建连接().checkConnectivity());
debugPrint(connectivityResult.toString());
if((connectivityResult==connectivityResult.mobile)| |(connectivityResult==connectivityResult.wifi)){
返回未来值(true);
}否则{
返回Future.value(false);
}
}
 static Future<bool> checkConnection() async{

    ConnectivityResult connectivityResult = await (new Connectivity().checkConnectivity());

    debugPrint(connectivityResult.toString());

    if ((connectivityResult == ConnectivityResult.mobile) || (connectivityResult == ConnectivityResult.wifi)){
      return Future<bool>.value(true);
    } else {
      return Future<bool>.value(false);
    }
  }