'type'_InternalLinkedHashMap<String,dynamic>'is not subtype of type 'List<dynamic>'

am try to build note app I have this error in my code also I have try a lot of different is the error on my phonecodes but the Error still shown in my phone screen

that my Model class

class Data {
  final int notes_user;
  final String title;
  final String constant;

  Data({required this.notes_user, required this.constant, required this.title});
  factory Data.fromJson(Map<String, dynamic> json) {
    return Data(
      notes_user: json['id'],
      constant: json['constant'],
      title: json['title'],
    );
  }
}

my API Get request

Future<List<Data>> getPosts() async {
    final response = await http.get(Uri.parse(get_notes), headers: header);

    if (response.statusCode == 200) {
      List jsonResponse = json.decode(response.body);
      return jsonResponse.map((data) => Data.fromJson(data)).toList();
    } else {
      throw Exception('Unexpected error occured!');
    }
  }
 Future<List<Data>>? futureData;
  @override
  void initState() {
    futureData = getPosts();
    super.initState();
  }

that is my body code

body: Center(
          child: FutureBuilder<List<Data>>(
            future: futureData,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                List<Data>? data = snapshot.data;
                return ListView.builder(
                    itemCount: data!.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                          height: 75,
                          color: Colors.white,
                          child: Center(child: Text(data[index].title)));
                    });
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");
              }
              // By default show a loading spinner.
              return CircularProgressIndicator();
            },
          ),

this my API that I have make it with Strapi backend in Postman there is no error so all my error is in snapshot that what am thing

{
    "data": [
        {
            "id": 1,
            "attributes": {
                "title": "ibrahim",
                "constant": "ibrahim is my first api post on my phone",
                "createdAt": "2022-05-09T15:44:26.580Z",
                "updatedAt": "2022-05-09T17:51:14.443Z",
                "publishedAt": "2022-05-09T16:15:15.108Z"
            }
        },
        {
            "id": 17,
            "attributes": {
                "title": "post api again",
                "constant": "post this api to strpi then let see if it is work or no",
                "createdAt": "2022-05-09T17:46:31.841Z",
                "updatedAt": "2022-05-09T18:02:42.708Z",
                "publishedAt": "2022-05-09T17:46:31.833Z"
            }
        },
        {
            "id": 26,
            "attributes": {
                "title": "ibraim",
                "constant": "i am here to show my api on my application so i hope it is work",
                "createdAt": "2022-05-16T08:27:40.300Z",
                "updatedAt": "2022-05-16T08:45:39.734Z",
                "publishedAt": "2022-05-16T08:45:39.036Z"
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 3
        }
    }
}


Solution 1:[1]

   Future<List<Data>> getPosts() async {
    final response = await http.get(Uri.parse(get_notes), headers: header);

    if (response.statusCode == 200) {
      List jsonResponse = json.decode(response.body)['data']; ======> "ADD THIS ['data']"
      return jsonResponse.map((data) => Data.fromJson(data)).toList();
    } else {
      throw Exception('Unexpected error occured!');
    }
  }

Solution 2:[2]

Try this code:

Future<List<Data>> getPosts() async {
    final response = await http.get(Uri.parse(get_notes), headers: header);

    if (response.statusCode == 200) {
      final jsonData = json.decode(response.body) as Map<String, dynamic>;
    final extractedData = jsonData['data'] as List;
        final List<Data> loadedDataList = [];
        for (var modelData in extractedData) {
          loadedDataList.add(Data.fromJson(modelData));
        }

      return loadedDataList;
    } else {
      throw Exception('Unexpected error occured!');
    }
  }
 

Also change Data model to:

class Data {
  List<Data>? data;
  Meta? meta;

  Data({this.data, this.meta});

  Data.fromJson(Map<String, dynamic> json) {
    if (json['data'] != null) {
      data = <Data>[];
      json['data'].forEach((v) {
        data!.add(new Data.fromJson(v));
      });
    }
    meta = json['meta'] != null ? new Meta.fromJson(json['meta']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    if (this.meta != null) {
      data['meta'] = this.meta!.toJson();
    }
    return data;
  }
}

class Data {
  int? id;
  Attributes? attributes;

  Data({this.id, this.attributes});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    attributes = json['attributes'] != null
        ? new Attributes.fromJson(json['attributes'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    if (this.attributes != null) {
      data['attributes'] = this.attributes!.toJson();
    }
    return data;
  }
}

class Attributes {
  String? title;
  String? constant;
  String? createdAt;
  String? updatedAt;
  String? publishedAt;

  Attributes(
      {this.title,
      this.constant,
      this.createdAt,
      this.updatedAt,
      this.publishedAt});

  Attributes.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    constant = json['constant'];
    createdAt = json['createdAt'];
    updatedAt = json['updatedAt'];
    publishedAt = json['publishedAt'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['constant'] = this.constant;
    data['createdAt'] = this.createdAt;
    data['updatedAt'] = this.updatedAt;
    data['publishedAt'] = this.publishedAt;
    return data;
  }
}

class Meta {
  Pagination? pagination;

  Meta({this.pagination});

  Meta.fromJson(Map<String, dynamic> json) {
    pagination = json['pagination'] != null
        ? new Pagination.fromJson(json['pagination'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.pagination != null) {
      data['pagination'] = this.pagination!.toJson();
    }
    return data;
  }
}

class Pagination {
  int? page;
  int? pageSize;
  int? pageCount;
  int? total;

  Pagination({this.page, this.pageSize, this.pageCount, this.total});

  Pagination.fromJson(Map<String, dynamic> json) {
    page = json['page'];
    pageSize = json['pageSize'];
    pageCount = json['pageCount'];
    total = json['total'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['page'] = this.page;
    data['pageSize'] = this.pageSize;
    data['pageCount'] = this.pageCount;
    data['total'] = this.total;
    return data;
  }
}

Solution 3:[3]

the error was in my model class that is the solve of my error

class Data {
  final int notes_user;
  final String title;
  final String constant;

  factory Data({required this.notes_user, required this.constant, required this.title});

  Data.fromJson(Map<String, dynamic> json) {
    return Data(
      notes_user: json['attributes']['notes_users'],
      constant: json['attributes']['constant'],
      title: json['attributes']['title'],
    );
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 M Alkhatib
Solution 2
Solution 3