Flutter : Show an Alert Dialog after an async Api call

flutter alertdialog not showing
flutter alertdialog height
flutter alert dialog
flutter alertdialog return value
flutter custom alert dialog
flutter dismiss alertdialog
flutter input dialog
flutter check if dialog is open

This is the code to get the Login response. If there is an error I want to show an Alert dialog saying that there was an error during Login.

Future<String> login(String username, String password) async {
    Map<String, dynamic> params = {
       'username': username,
       'password': password,
    };

    final response = await http.post('apiurl', body: params);

    if (response.statusCode != 200)
       throw Exception(response.body);

    return response.body;
}

I'm adding the code from where the call to login happens. There is a TODO in the _loginController where I want to Show an Alert Dialog which shows the error.

class LoginWidgetState extends State<LoginWidget> {
  var _usernameController = new TextEditingController();

  var _passwordController = new TextEditingController();

  void _loginButtonClickHandler() {
    var username = _usernameController.text;
    var password = _passwordController.text;

    login(username, password).then((response) {

    }).catchError((e) {
      //TODO : show an Alert Here
    });
  }

  @override
  Widget build(BuildContext context) {
    var container = Center(
      child: Container(
    child: Column(
      children: <Widget>[
        TextField(
          decoration: InputDecoration(
            hintText: "username",
          ),
          controller: _usernameController,
        ),
        TextField(
          obscureText: true,
          decoration: InputDecoration(hintText: "password"),
          controller: _passwordController,
        ),
        RawMaterialButton(
          onPressed: _loginButtonClickHandler,
          child: Text("Login"),
        )
       ],
     ),
    ),
   );

   return container;
  }

}

Refer here to show the dialog.

Send context to _loginButtonClickHandler and you are done. Cheers

Manager your Flutter Dialogs with a Dialog Manager, After we'll use it to implement the architecture around our dialogs. The calling code will await like a normal future until you call complete, this means we can wait for user input to be It will have a basic API. The listener it will use the Alert widget from rflutter_alert to show a basic dialog with a button. After completing this tutorial, you will know different use case of dialog in a Flutter: Flutter popup AlertDialog example. AlertDialog with a Button. SimpleDialog for select options. AlertDialog with TextField(EditText) for input. 1.Acknowledgement popup AlertDialog. Popup dialog informs the user about situations that require acknowledgment.

This will help you!

Future<String> login(String username, String password) async {
Map<String, dynamic> params = {
   'username': username,
   'password': password,
};

final response = await http.post('apiurl', body: params);

if (response.statusCode != 200)
{
  throw Exception(response.body);
}
else 
{
  showWhateverFunction();
}
return response.body;

}

Flutter AlertDialog example, Flutter AlertDialog and SimpleDialog is a small widget that use to After completing this tutorial, you will know different use case of _asyncConfirmDialog(BuildContext context) async { Simple dialogs display a list of items that take immediate effect when It is only used when the method is called. name: flutter_api_calls description: Flutter application to demonstrate api calls. # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +.

You can use RFlutter Alert library for that. It is easily customizable and easy-to-use alert/popup dialog library for Flutter. I hope it will help you.

Example alert with RFlutter Alert:

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

*I'm one of developer of RFlutter Alert.

showDialog setState after await doesn't work · Issue #20372 · flutter , setState re-build the State that called it. Feedback is on the dialog, that is, it is on another route. Create your first API Call in Flutter. to make a http get call; dart:async : to make future async call. dart it will iterate and add the data to the ListView.Builder and show the results.

SimpleDialog not showing after call showDialog · Issue #17337 , If I call displayDialog, the Dialog cannot show, but the screen is dimmed, SimpleDialog not showing after call showDialog #17337 async{ await showDialog(context: context, builder: (BuildContext This goes same with AlertDialog and SimpleDialog Is this still an issue with recent Flutter versions? A material design alert dialog. An alert dialog informs the user about situations that require acknowledgement. An alert dialog has an optional title and an optional list of actions. The title is displayed above the content and the actions are displayed below the content. If playback doesn't begin shortly, try restarting your device.

AlertDialog class - material library - Dart API, API docs for the AlertDialog class from the material library, for the Dart programming language. This snippet shows a method in a State which, when called, displays a dialog box and returns a Future<void> _showMyDialog() async { return showDialog<void>( context: context, 'This feature was deprecated after v1.13.2. Flutter Alert Dialogs An alert dialog is a useful feature that notifies the user with important information to make a decision or provide the ability to choose a specific action or list of actions. It is a pop-up box that appears at the top of the app content and the middle of the screen.

Flutter Alert Dialogs, Flutter Alert Dialogs with What is Flutter, Installation, Testing, Flutter First Application, Android Platform-Specific Code> Flutter Packages Flutter REST API Flutter To show an alert, you must have to call showDialog() function, which contains the _displayDialog(BuildContext context) async {; return showDialog(; context:  The listener it will use the Alert widget from rflutter_alert to show a basic dialog with a button. onClosed we want to notify the service that the dialog has been closed and when the button is

Comments
  • I want to have common method to make an Api call. In that case I just want to throw the Exception and leave it to the caller to handle it accordingly.
  • then try try catch!
  • I'm already throwing it. I just need to complete the TODO part