简体   繁体   English

Flutter:检查 Internet 连接并根据输出进行导航?

[英]Flutter: Check Internet connectivity and navigate based on output?

I am new to flutter, I want to check whether the internet is available or not and based on the condition, the screen has to change.我是新手,我想检查互联网是否可用,根据情况,屏幕必须改变。 I have written the code below (screen switch is working properly), but I am not able to get bool output(internet).我已经编写了下面的代码(屏幕开关工作正常),但我无法获得 bool 输出(互联网)。 When I removed the Future in check internet class, it throws an error.当我在 check internet 类中删除 Future 时,它​​会引发错误。 Can you please solve the issue:你能解决这个问题吗:

class _ScreenState extends State<ChannelScreen> {
bool isInternet;
bool result;

  @override
  void initState() {
    // TODO: implement initState
    result = check();
    super.initState();
  }

  @override

  Widget _buildChild() {


        print ("The output “);
        print (result);
       if (result != Null && result == true) {
// if internet is ON
return Container();
                }
//if internet is off
       return Container();
  }


  Widget build(BuildContext context) {
    return new Container(child: _buildChild());
  }
}


Future<bool> check()   async{
  var connectivityResult =   await Connectivity().checkConnectivity();
  if (connectivityResult == ConnectivityResult.mobile) {
    print ("******* Mobile is ON ******");
    return true;
  } else if (connectivityResult == ConnectivityResult.wifi) {
    print ("******* Wifi is ON ******");
    return true;
  }
  print ("No connectivity");
  return false;
}

You can use StreamBuilder您可以使用StreamBuilder

StreamBuilder(
  stream: Connectivity().onConnectivityChanged,
  builder: (context, snapshot) {
    // Use this to avoid null exception
    if (snapshot.connectionState == ConnectionState.none) {
      return CircularProgressIndicator();
    } else {
      ConnectivityResult result = snapshot.data;

      // Check Connectivity result here and display your widgets
      if(ConnectivityResult.none) {
        yourWidgetForNoInternet();
      } else {
        yourWidgetForInternet();
      }
    }
  },
)
bool hasInternet = false, isChecking = true;

  @override
  void initState() {
    super.initState();
    check();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: isChecking
            ? ListTile(
                leading: CircularProgressIndicator(), title: Text('Checking...'))
            : hasInternet
                ? ListTile(title: Text('Your widget here...'))
                : ListTile(
                    leading: Icon(Icons.info),
                    title: Text('No Internet Conncetion')));
  }

  check() async {
    var connectivityResult = await Connectivity().checkConnectivity();
    if (connectivityResult == ConnectivityResult.mobile) {
      print("******* Mobile is ON ******");
      setState(() {
        isChecking = false;
        hasInternet = true;
       //navigate to another screen.....
      });
    } else if (connectivityResult == ConnectivityResult.wifi) {
      print("******* Wifi is ON ******");
      setState(() {
        isChecking = false;
        hasInternet = true;
        //navigate to another screen.....
      });
    } else {
      setState(() {
        isChecking = false;
        hasInternet = false;
      });
    }
  }

There is a plugin to use connectivity:有一个插件可以使用连接:

https://pub.dev/packages/connectivity#-readme-tab- https://pub.dev/packages/connectivity#-readme-tab-

I use following code to check whether the internet is connected or not我使用以下代码检查互联网是否已连接

static Future<bool> checkInternetConnectivity() async {
    bool isConnected;
    try {
      final result = await InternetAddress.lookup('google.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        isConnected = true;
      }
    } on SocketException catch (_) {
      isConnected = false;
    }
    return isConnected;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM