2

My name is Pablo and I am currently building a Flutter app. So, my app gets some images and audios from Firebase Storage, and obviously, without the internet connection, the app doesn't display the images and doesn't play the audios. I want the app to pop-up an alert to the user when there is no WIFI nor Data; how can I do that?

Thanks :)

Serl
  • 240
  • 1
  • 14

3 Answers3

4

You can simply use a function to check if you have network connection, by pinging Google servers:

/system/bin/ping -c 1 8.8.8.8

In Android, this function looks like this:

public boolean isNetworkAvailable() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process process = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int exitValue = process.waitFor();
        return (exitValue == 0);
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
    return false;
}

In Firestore, offline persistence is enabled by default. So you can check if the user reads data from the cache or from Firebase servers. A more elegant way would be to use isFromCache() function. This is the code for Android:

yourDocRef.addSnapshotListener(new DocumentListenOptions().includeMetadataChanges(), new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
        Log.d("listener.isFromCache: " + documentSnapshot.getMetadata().isFromCache());
    }
});
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you so much! – Pablo de la Fuente Sep 11 '18 at 23:26
  • @Alex Mamo: I just copied and pasted your first approach with `isNetworkAvailable()` and I always get a false, altough I have Internet connection. Any idea why this is happening? – VanessaF Nov 12 '21 at 17:02
  • @VanessaF Most likely because you aren't reaching Goole servers. If you ping from the command prompt, are you getting the same behavior? – Alex Mamo Nov 12 '21 at 17:10
  • I used "ping 8.8.8.8" in windows 5 times. 2 times 4/4 packages were sent. 2 times 1/4. And one time 3/4. But with your suggested method in Java I always get a false (I tried it more than 30 times) – VanessaF Nov 12 '21 at 17:17
  • I tried another 50 times with your suggested method and meanwhile use the windows ping. While in the windwons ping I always get at least 1 out of 4 packages for ping 8.8.8.8 or ping www.google.com, your method always yiels false. The method itself is executed (I know this because I check with E/LogTag). Any idea what the problem might be? I also checked the Android manuscript and there I have allowed INTERNET connections. – VanessaF Nov 12 '21 at 17:34
  • @VanessaF Without seeing it, I cannot say what it could be. – Alex Mamo Nov 13 '21 at 11:23
  • @AlexMamo: Thanks Alex for your comment. I created a new question with additional information to my problem: https://stackoverflow.com/questions/69953789/qemu-pipe-open-ns62-could-not-connect-to-the-pipeqemudnetwork-service-inv. Maybe you can have a look at this? – VanessaF Nov 13 '21 at 11:27
  • @AlexMamo: Could you have a look at my question here (https://stackoverflow.com/questions/69953789/network-connection-check-in-android-always-returns-false-altough-network-is-avai?noredirect=1#comment123707133_69953789). I apply your suggested code but it always returns false. Do you have any idea how to solve this problem? – VanessaF Nov 16 '21 at 20:10
  • i know this is old question but trust me @Alex Mamo .. isFromCache() not always work 100% , i have fully past pains of this function – Mohammed Hamdan Jul 09 '22 at 11:41
1

You can use the connectivity package https://pub.dartlang.org/packages/connectivity Here is a simple tutorial with the dart file attached https://medium.com/@aseemwangoo/internet-connectivity-in-flutter-a6b6aedf2964

Possible duplicate - Check whether there is an Internet connection available on Flutter app

F-1
  • 2,887
  • 20
  • 28
0

For showing alert part, you can use RFlutter Alert library. It is easily customizable and easy-to-use alert/popup dialog library for Flutter.

Example code:

Alert(context: context, title: "RFLUTTER", desc: "Flutter is awesome.").show();

*I'm one of developer of RFlutter Alert.

ibrahimdevs
  • 163
  • 7