美文网首页
Flutter网络是否可用监听

Flutter网络是否可用监听

作者: 郑永博 | 来源:发表于2019-09-25 10:01 被阅读0次
import 'dart:io';

import 'compile_utils.dart';

class NetworkUtils {
  static Future<bool> isNetworkAvailable() async {
    try {
      String host = "google.com";//判断国内外,谷歌还是百度
      if (CompileModel.getCompileUrl() == CompileModel.urls[0]) {
        host = "baidu.com";
      } else {
        host = "google.com";
      }
      final result = await InternetAddress.lookup(host);
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        print("Network is ok.");
        return true;
      }
    } on SocketException catch (_) {
      print("Network is error.");
      return false;
    }
  }
}

相关文章

网友评论

      本文标题:Flutter网络是否可用监听

      本文链接:https://www.haomeiwen.com/subject/bhulyctx.html