如何在飞镖中的不同线程中运行流

发布于 2025-02-13 23:58:35 字数 1623 浏览 0 评论 0原文

Future<bool> checkConnection() async {
  var connectivityResult = await (Connectivity().checkConnectivity());

  bool connOk = false;

  if (connectivityResult == ConnectivityResult.mobile) {
    bool ii = await checkConnectionWithUrl();
    if (ii == true) {
      connOk = true;
    }
  } else if (connectivityResult == ConnectivityResult.wifi) {
    bool ii = await checkConnectionWithUrl();
    if (ii == true) {
      connOk = true;
    }
  } else if (connectivityResult == ConnectivityResult.ethernet) {
    bool ii = await checkConnectionWithUrl();
    if (ii == true) {
      connOk = true;
    }
  } else if (connectivityResult == ConnectivityResult.bluetooth) {
    bool ii = await checkConnectionWithUrl();
    if (ii == true) {
      connOk = true;
    }
  } else {
    connOk = false;
  }

  return connOk;
}

Future<bool> checkConnectionWithUrl() async {
  bool response = false;
  while (true) {
    var checkin = await client.get(Uri.https('www.google.com', '/'));
    if (checkin.statusCode == 200) {
      response = true;
      break;
    } else {
      sleep(const Duration(milliseconds: 500));
    }
  }
  return response;
}


Stream checkStream() async* {
  dynamic result = false;
  while (true) {
    try {
      result = await checkConnection();
    } catch (error) {
      result = false;
    } finally {
      yield result;
    }
    await Future.delayed(const Duration(seconds: 1));
  }
}

我正在制作一个应用程序。我想学习DART中的多线程。以上功能将要做的是每秒寻找WiFi状态并更新UI。但是,当我运行该功能时,它将在UI中删除Frams。因此,我可以在不同的线程中运行它。我可以吗

---我的主要目标---
我想在其他线程中运行此功能

Future<bool> checkConnection() async {
  var connectivityResult = await (Connectivity().checkConnectivity());

  bool connOk = false;

  if (connectivityResult == ConnectivityResult.mobile) {
    bool ii = await checkConnectionWithUrl();
    if (ii == true) {
      connOk = true;
    }
  } else if (connectivityResult == ConnectivityResult.wifi) {
    bool ii = await checkConnectionWithUrl();
    if (ii == true) {
      connOk = true;
    }
  } else if (connectivityResult == ConnectivityResult.ethernet) {
    bool ii = await checkConnectionWithUrl();
    if (ii == true) {
      connOk = true;
    }
  } else if (connectivityResult == ConnectivityResult.bluetooth) {
    bool ii = await checkConnectionWithUrl();
    if (ii == true) {
      connOk = true;
    }
  } else {
    connOk = false;
  }

  return connOk;
}

Future<bool> checkConnectionWithUrl() async {
  bool response = false;
  while (true) {
    var checkin = await client.get(Uri.https('www.google.com', '/'));
    if (checkin.statusCode == 200) {
      response = true;
      break;
    } else {
      sleep(const Duration(milliseconds: 500));
    }
  }
  return response;
}


Stream checkStream() async* {
  dynamic result = false;
  while (true) {
    try {
      result = await checkConnection();
    } catch (error) {
      result = false;
    } finally {
      yield result;
    }
    await Future.delayed(const Duration(seconds: 1));
  }
}

I am making a app. And I want to learn multi threading in dart. The above function what it will do is it look for wifi state every second and update the Ui. But when I run the function it will drop frams in ui. So I what to run it in a diffrent thread. can i do it.

---My main goal ---
I want to run this function in a different thread how do I do it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文