skip to Main Content

I want submit Selected answers array how can do this.

How to pass selected answer in this array




2

Answers


  1. Flutter/Dart doesn’t have arrays. Use lists instead.

    Login or Signup to reply.
  2. Solution

    In the api-response ,it’s always formatted into either List of Map or Map of List.

    List of Map is Like :

    [
    
     {
       "name":"ruby",
       "gender":"female"
     },
    
     {
       "name":"otis",
       "gender":"male"
     },
    
    ]
    

    And, Map of List is Like :

    {
        "data": [
            "Professor",
            "Berlin",
            "Tokyo"
        ]
    }
    

    So to access them you have to use JsonDecode and then look the response and process that.

    For Map response ….

    var resMap = jsonDecode(response.body);
    
    

    For List response (use key after resMap with key)…

    var resMap = jsonDecode(response.body);
    var infoList = resMap["data"];
    

    Demo

    class CatTypeController {
      Future<void> getLeaves() async {
        String url = "https://meowfacts.herokuapp.com/?count=2";
    
        http.Response response = await http.get(Uri.parse(url));
    
        if (response.statusCode == 200) {
          Map rM = jsonDecode(response.body);
          print(rM["data"]);
        }
      }
    }
    

    Response

    ezgif-4-42fcba32ba

    Output

    ["In ancient Egypt, killing a cat was a crime punishable by death.","The way you treat kittens in the early stages of it's life will render it's personality traits later in life."]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search