Home > Software design >  list<dynamic> is not a subtype of type FutureOr<List<Map<String,dynamic>> error
list<dynamic> is not a subtype of type FutureOr<List<Map<String,dynamic>> error

Time:11-02

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...

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']));
                  });
            }
        }
      },
    );
}}

CodePudding user response:

  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

CodePudding user response:

Your API Call:

Future<List<dynamic>> getJobsData() async {
    String url = 'https://jsonplaceholder.typicode.com/photos';
    var response = await http.get(Uri.parse(url), headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    });
    return json.decode(response.body);
  }

Your Widget:

Center(
  child: FutureBuilder<List<dynamic>>(
    future: getJobsData(),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListView.builder(
            itemCount: snapshot.data!.length,
            itemBuilder: (context, index) {
              var title = snapshot.data![index]['title'];
              
               return Card(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(15.0),
                ),
                child: ListTile(
                  title: Text(title),
                 ),
              );
            },
          ),
        );
      }
      return CircularProgressIndicator();
    },
  ),
),

Result-> image

  • Related