title: Flutter : Capture the Future response from a http call as a normal List
body: <p>I am trying to make my previously static app, dynamic by making calls to the backend server. </p>
  
  <p>This is what my service looks like </p>
  
  <pre><code>String _url = "http://localhost:19013/template/listAll";
  
  Future&lt;List&lt;TemplateInfoModel&gt;&gt; fetchTemplates() async {
    final response =
    await http.get(_url);
    print(response.statusCode);
  
    if (response.statusCode == 200) {
      // If the call to the server was successful, parse the JSON
      Iterable l = json.decode(response.body);
      List&lt;TemplateInfoModel&gt; templates = l.map((i) =&gt; TemplateInfoModel.fromJson(i)).toList();
      return templates;
    } else {
    // If that call was not successful, throw an error.
    throw Exception('Failed to load post');
    }
  }
  </code></pre>
  
  <p>And my model looks like this :</p>
  
  <pre><code>class TemplateInfoModel {
    final String templateId;
    final String messageTag;
    final String message;
    final String messageType;
  
  
    TemplateInfoModel({this.templateId, this.messageTag, this.message,
      this.messageType});
  
    factory TemplateInfoModel.fromJson(Map&lt;String, dynamic&gt; json) {
      return TemplateInfoModel ( templateId: json['templateId'], messageTag : json['messsageTag'], message : json ['message'] , messageType : json['messageType']);
    }
  
  }
  </code></pre>
  
  <p>I have a utils method within which I am capturing the http request/response data; which would then use that to create a DropDown Widget ( or display it within a text) </p>
  
  <p>My earlier dummy data was a list; I am wondering how best I can convert this Future> to List </p>
  
  <pre><code>class SMSTemplatingEngine {
  
      var _SMSTemplate; //TODO this becomes a Future&lt;List&lt;TemplateInfoModels&gt;&gt;
     // TemplateInfoService _templateInfoService;
  
      SMSTemplatingEngine(){
        _SMSTemplate=fetchTemplates();
      }
  
  //  var _SMSTemplate = {
  //    'Reminder':
  //        'Reminder : We’re excited to launch a new feature on our platform that will revolutionize your Facebook marketing and triple your ROI. Visit url.com to learn more',
  //    'New Message':
  //        'New Message: We’re excited to launch a new feature on our platform that will revolutionize your Facebook marketing and triple your ROI. Visit url.com to learn more',
  //    'Connecting Again':
  //        'Connecting Again : We’re excited to launch a new feature on our platform that will revolutionize your Facebook marketing and triple your ROI. Visit url.com to learn more',
  //  };
  
    List&lt;String&gt; getKeys(){
      List&lt;String&gt; smsKeys = new List();
      for ( var key in _SMSTemplate.keys)
        smsKeys.add(key);
      return smsKeys;
    }
  
    String getValuePerKey(String key){
      return _SMSTemplate['${key}'];
    }
  
  
  }
  </code></pre>
  
  <p>P.S. I have looked at some posts but I was completely bowled over since I am a Flutter Newbie. </p>
  
  <p>Is there an easier way for this to happen. </p>
  
  <p>Thanks,</p>
  
  <p> </p>
  
  <p>The widget which would display the content from the http call </p>
  
  <pre><code>   var templateMessagesDropDown = new DropdownButton&lt;String&gt;(
        onChanged: (String newValue) {
          setState(() {
            templateMsgKey = newValue;
            print("Selcted : ${templateMsgKey.toString()} ");
          });
        },
        // value: _defaultTemplateValue,
        style: textStyle,
        //elevation: 1,
        hint: Text("Please choose a template"),
        isExpanded: true,
        //
        items: smsEngine.getKeys().map&lt;DropdownMenuItem&lt;String&gt;&gt;((String value) {
          return DropdownMenuItem&lt;String&gt;(
            value: value,
            child: Text(value),
          );
        }).toList(),
      );
  </code></pre>