2

I'm new to Flutter and connectivity. I try connectivity package it works but I want to implement in existing app. If there is connection available then open HomePage() state, otherwise refer to another state. Please help me.

Here's my code https://pastebin.com/3wbDiF8j (main.dart). https://pastebin.com/vPUYUdgc (noInternet.dart)

M.K. Mandawar
  • 69
  • 1
  • 12
  • 1
    What did you try? Where exactly are you stuck? Can you post your best try and the problems or errors messages you get? – nvoigt Jun 18 '21 at 06:45
  • @nvoigt Exactly I want that if user connected to the internet then open main state/page, otherwise refer to another page. like No Internet page – M.K. Mandawar Jun 18 '21 at 06:48
  • 2
    If you just "want" that, you need to hire a software developer to do it for you. This site is about helping another developer with their problems. You have not yet posted what problems you face. What is it, that is not working for you? What does your code look like and where exactly do you need help in developing this yourself? – nvoigt Jun 18 '21 at 06:50
  • I am unable to implement in existing code – M.K. Mandawar Jun 18 '21 at 06:51
  • 2
    Well, sorry to say, but then you need to hire someone. We don't do the work for you. We help you with your problems doing it. Again: you need to *try it for yourself*, then *post that best effort here* with the problems you have with it. "I don't know how please do it for me" will not cut it here, that's not how this site works. So can you provide an [mcve] of navigating between pages? Can you read the current status of the internet connection with the package? – nvoigt Jun 18 '21 at 06:53
  • There is good code in the [example](https://pub.dev/packages/connectivity/example) - just adapt it into your own code. If it doesn;t work, then post your code & we will help. But we will not write your code for you – Mawg says reinstate Monica Jun 18 '21 at 06:54
  • @nvoigt google is actively marketing flutter for first time developers. I would not blame it on him but on google. There are lots of similar questions. – Ced Jun 18 '21 at 06:57
  • 1
    @nvoigt here is my main.dart https://pastebin.com/3wbDiF8j and no internet.dart https://pastebin.com/vPUYUdgc – M.K. Mandawar Jun 18 '21 at 07:04

2 Answers2

7

Use Connectivity Plus package and return MaterialApp using StreamBuilder. In this way, you won't have to check for connection on every single page.

StreamBuilder(
        stream: Connectivity().onConnectivityChanged,
        builder: (context, AsyncSnapshot<ConnectivityResult> snapshot) {
          return snapshot.data == ConnectivityResult.mobile ||
                  snapshot.data == ConnectivityResult.wifi
              ? OnlineMaterialApp()
              : OfflineMaterialApp();
        },
      ),
Nandan Wewhare
  • 445
  • 5
  • 11
0

For a starter, You can listen for changes, for example in your onInit method at the top of your app:

connectivity.onConnectivityChanged
        // .distinct()  (from rxdart package, uncomment if you have this dep)
        // debounce time for epileptic network
        // .debounceTime(_debounceTime)  (from rxjs package)
        .listen((c) => setState(() => connectivity = c));

in your build method you can now use the value.

This has to wrap your applications pages, to do so you can set the above listener in a widget that will display the right child depending on the connectivity state and use that widget in the build method:

MaterialApp(
  // ...
  builder: (ctx, child) => ConnectivityWrapper(child)
)

and in ConnectivityWrapper onInit

connectivity.onConnectivityChanged
        // .distinct()  (from rxjs package, uncomment if you have this dep)
        // .debounceTime(Duration(milliseconds: 400))  (from rxjs package)
        .listen((c) => setState(() => connectivity = c));

In connectivity wrapper build:

if (connectivity == ConnectivityResult.none) 
  return NoInternetPage();
return child;
Ced
  • 15,847
  • 14
  • 87
  • 146