0

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 ??

OceanL
  • 469
  • 1
  • 5
  • 21

1 Answers1

2

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

digitaljoni
  • 1,357
  • 1
  • 9
  • 12
  • but this pub does not support web flutter – OceanL Aug 27 '19 at 02:42
  • in that case, try this solution here: https://stackoverflow.com/a/49648870/11216418 basically, you will use try..catch and on SocketException... it means that there was an issue connecting to your server – digitaljoni Aug 27 '19 at 03:38
  • Currently, it does support the web too. I suggest to you @digitaljoni to add the basic example of it. – Reiko Dev Oct 09 '21 at 19:25