Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

list<dynamic> is not a subtype of type FutureOr<List<Map<String,dynamic>> error in flutter

I have been trying with last an hour but not getting solution and failing completely to understand why its showing an error…

I have created a function for fetching data,

I have placed print statement for seeing what does it returns…here it is printing data but while inside feature builder it showing an error…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

when I run app its showing output with


list<dynamic> is not a subtype of type FutureOr<List<Map<String,dynamic>>

it means its executes snapshot.haserror part

here is my code


class _HomeScreenState extends State<HomeScreen> {
  Future<List<Map<String,dynamic>>> fetchdata() async {
    var resp =
    await http.get(Uri.parse("https://jsonplaceholder.typicode.com/photos"));

   print("fetchdata function showing"+json.decode(resp.body).toString());

    return json.decode(resp.body);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: MyBody(),
      ),
    );
  }

  MyBody() {
    return FutureBuilder<List<Map<String,dynamic>>>(
      future: fetchdata(),
      builder: (context, snapshot) {
        print("Futurebuilder showing:"+snapshot.toString());

        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return Center(child: CircularProgressIndicator());
          default:
            if (snapshot.hasError) {
              return Text('Error: ${snapshot.error}');
            } else {

              print('againt'+snapshot.toString());
              List<Map<String,dynamic>> data = snapshot.data ?? [];
              return ListView.builder(
                  itemCount: data.length,
                  itemBuilder: (context, index) {
                    return Container(
                        padding: EdgeInsets.all(8.0),
                        child: Text(data[index]['title']));
                  });
            }
        }
      },
    );
}}

>Solution :

  Future<List<Map<String, dynamic>>> fetchdata() async {
    var resp = await http
        .get(Uri.parse("https://jsonplaceholder.typicode.com/photos"));

    print("fetchdata function showing" + json.decode(resp.body).toString());
    List<dynamic> result = jsonDecode(resp.body);
    return result.map((e) => e as Map<String, dynamic>).toList();
  }

just change your function like this

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading