despo / issuehub.io

Find open source issues to work on! (domain update)
http://issuehub.pro
MIT License
261 stars 174 forks source link

Problem when fetching json data from internet in flutter #203

Closed yodaljit closed 4 years ago

yodaljit commented 5 years ago

I'm new to flutter and was running a code to fetch and display data from internet. But when I'm running the code I'm getting this error: List<dynamic>' is not a subtype of type 'Map<String, dynamic>. I tried to find out the solution but nothing is of help. Thanks in advance.

Here is my code:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;

class Message extends StatefulWidget {
  @override
  _MessageState createState() => _MessageState();
}

class _MessageState extends State<Message> {
  Map data;
  List userData;
  Future getData() async
  {
    http.Response response = await http.get('https://jsonplaceholder.typicode.com/users');

    data = json.decode(response.body);

    setState(() {
     userData = data["data"];
    });

  }

  @override
  void initState(){
    super.initState();
    getData();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
          child:ListView.builder
          (
            shrinkWrap: true,
            itemCount: userData == null ? 0 : userData.length,
              itemBuilder:(BuildContext context, int index){
                return Row(
                  children:<Widget>[

                Text('${userData[index]["username"]}', style: TextStyle(fontWeight: FontWeight.w600, fontSize: 20),),
                Text('${userData[index]["catchPhrase"]}',style: TextStyle(fontWeight: FontWeight.w500, fontSize: 17),),
                Icon(Icons.navigate_next)
                  ]
                  );
                   }

          )
    );
  }
}
despo commented 4 years ago

Not relevant to issuehub.