Home > Software engineering >  type '_internalLinkedHashMap<String,dynamic> is not a subtype of type list <dynamic>
type '_internalLinkedHashMap<String,dynamic> is not a subtype of type list <dynamic>

Time:01-04

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

Future<List<Data>> fetchData() async {
  final response = await http.get(Uri.parse(
      'http://www.omdbapi.com/?s=can&y=2018&type=movie&apikey=9f4f767e'));

  if (response.statusCode == 200) {
    List jsonResponse = json.decode(response.body);

    return jsonResponse.map((data) => new Data.fromJson(data)).toList();
  } else {
    throw Exception('Unexpected error occured!');
  }
}

class Data {
  late String title;
  late int year;
  late int imdbID;
  late String type;

  Data(
      {required this.title,
      required this.year,
      required this.imdbID,
      required this.type});

  Data.fromJsonN(Map<String, dynamic> json) {
    title = json['title'];
    year = json['year'];
    imdbID = json['imdbID'];
  }

  factory Data.fromJson(Map<String, dynamic> json) {
    return Data(
      title: json['title'],
      year: json['year'],
      imdbID: json['imdbID'],
      type: json['type'],
    );
  }
}

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late Future<List<Data>> futureData;

  @override
  void initState() {
    super.initState();

    futureData = fetchData();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter movie API and ListView Example',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter movie ListView'),
        ),
        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();
            },
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

You can solve that problem to set jsonResponse as List dynamic

List<Data> dataResponseModel = (jsonResponse['data'] as List<dynamic>)
              .map((data) => Data.fromJson(data))
              .toList();


return dataResponseModel;

CodePudding user response:

Actually the problem is that you are passing Map<String, dynamic> where you have to pass a List. When you decode your json string it gives you a Map like :

{
 Search : [{You data}]
}

But you need the a List then you have to select search that will return you a list so make a little change to your code like below :

List jsonResponse = json.decode(response.body)["Search"];
  •  Tags:  
  • Related