Home > Mobile >  Getting this persistent error. List<dynamic> is not a subtype of type Map<String, dynamic&g
Getting this persistent error. List<dynamic> is not a subtype of type Map<String, dynamic&g

Time:08-04

This is my model class

import 'dart:convert';

Streams streamsFromJson(String str) => Streams.fromJson(json.decode(str));

String streamsToJson(Streams data) => json.encode(data.toJson());

class Streams {
 Streams({
   this. Title,
   this. Stream,
   this. Description,
   this.author,
   this.availability,
 });

 String? title;
 String? stream;
String? description;
 String? author;
 bool? availability;

 factory Streams.fromJson(Map<String, dynamic> json) => Streams(
       title: json["title"],
       stream: json["stream"],
       description: json["description"],
       author: json["author"],
       availability: json["availability"],
     );

 Map<String, dynamic> toJson() => {
      "title": title,
      "stream": stream,
      "description": description,
      "author": author,
      "availability": availability,
    };

}

And this is how I am fetching the data

class RetrieveStreams {
static var client = http.Client();
ProjectApis projectApis = ProjectApis();

static Future<Streams> fetchStreams() async {
  final response = await client. Get(
    Uri.parse(ProjectApis.streamsUrl),
  );
  if (response.statusCode == HttpStatus.ok) {
    // Call is successful 
    var jsonString = response. Body;
    return streamsFromJson(jsonString);
  } else {
    // If that call was not successful, throw an error.
    Get.snackbar(
      'Error',
      'Failed to load streams',
       backgroundColor: Colors.red,
      colorText: Colors.white,
     );
     throw Exception('');
    }
  }
}

This is my controller class using getx

lass StreamsController extends GetxController {
 var streamList = <Streams>[].obs;

 void getStreams() async {
   var streamVariable = await RetrieveStreams.fetchStreams();
   streamList.value = streamVariable as List<Streams>;
 }

    @override
    void onInit() {
      getStreams();
   super.onInit();
  }
}

And I am showing the data in this Text widget

Text(
   _streamsController.streamList
    .map((element) => element. Title)
     .toList()[index]
     .toString(),
      style: TextStyle(
      color: ProjectColors.black,
    ),
 ),

I have read through this enter link description here carefully but it does not seem to solve my problem.

The Error seems to suggest that the data I am receiving from the endpoint is in the form of a list and I am using a map, suggesting a data mismatch. I just can't seem to get my head around this error

CodePudding user response:

It looks like the API is returning an array instead of a Streams object. Supposing it's an array of Streams objects, to encode/decode this array use the following functions:

List<Streams> streamsListFromJson(String str) =>
    (json.decode(str) as List<dynamic>)
        .map((s) => Streams.fromJson(s))
        .toList();

String streamsListToJson(List<Streams> data) =>
    json.encode(data.map((s) => s.toJson()).toList());
  • Related