sparklingice11
sparklingice11

Reputation: 11

display fetched value in alert dialog

I want to display fetched value/s from db in alert dialog. Here's the snippet where I fetched the data/s:

 final String url = "http://12.10.10.3/proj/fetch.php";

  List data = List(); 


  Future<String> getSWData() async {
    var res = await http
        .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
    var resBody = json.decode(res.body);

    setState(() {
      data = resBody;
    });
   
  }

And here's the simple alert dialog code snippet.

 showAlertDialog(BuildContext context) {
    
      Widget okButton = FlatButton(
        child: Text("OK"),
        onPressed: () { 
       Navigator.maybePop(context,true);
         },
      );

     
      AlertDialog alert = AlertDialog(
        title: Text("My  title"),
        content:  Text("Sample text"),
        actions: [
          okButton,
        ],
      );

   
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return alert;
        },
      );
    }

I have 2 columns in my Db which are value and description. I tried putting data['description'] on the alert dialog content. But it gives me argument not assignable error. Can someone help me. I'm new to flutter

Upvotes: 1

Views: 731

Answers (2)

Akif
Akif

Reputation: 7660

You need to use StatefulBuilder for AlertDialog Widget. AlertDialog is a stateless widget. This is the reason.

   StatefulBuilder(
       builder: (context, setState) {
           return AlertDialog(
                 title: Text("My  title"),
                 content:  Text("Sample text"),
                      actions: [
                                 okButton,
                               ],
                          );

You can read more from this blog.

Upvotes: 0

Raoul M&#252;ller
Raoul M&#252;ller

Reputation: 405

You need to wrap data['description'] with a Text widget since the content parameter expects a widget.

Upvotes: 1

Related Questions