OceanL
OceanL

Reputation: 479

show dialog when there is a problem in the internet network

I mean when I log in, but the network to the database there is a problem, an information dialog will appear that the network has a problem. I'm confused about using else if here..

    _login() async {
    if (formKey.currentState.validate()) {
      formKey.currentState.save();
      try {
        final response = await UserController.login({
          "username": username,
          "password": password,
        });

        if (response != null && response["success"]) {
          Http.setAccessToken(response["token"]);

          return Navigator.pushReplacement(
              context,
              MaterialPageRoute(
                builder: (context) => MainActivity(),
              ));
        } else {
          await showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text("Information"),
                content: Text("Your account is not registered!"),
                actions: <Widget>[
                  FlatButton(
                    child: Text("Close"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            },
          );
        }
      } catch (e) {
        print(e.message);
      }
    }
  }

how do I put conditions if the network has a problem then return showDialog ??

Upvotes: 0

Views: 1885

Answers (1)

digitaljoni
digitaljoni

Reputation: 1397

Check out the connectivity package from the Flutter Team. https://pub.dev/packages/connectivity

You can listen for any issues in changes to your network then show a snackbar or dialogbox if disconnected.

I hope this helps.

JC

Upvotes: 2

Related Questions