Home > Software engineering >  _InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynam
_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynam

Time:08-10

I am facing this problem

E/flutter ( 3688): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable'

I want to get data from the api in listview bilder. but this exception which i am not familiar with. How to solve it i donot know.

import 'dart:convert';

import 'package:flutter/material.dart';

import 'package:learning_1ui_6228/utilities/app_colors.dart';
import 'package:learning_1ui_6228/utilities/helper.dart';
import 'package:learning_1ui_6228/utilities/widgets/app_line.dart';

import 'package:learning_1ui_6228/utilities/widgets/list_tile_widget.dart';

import 'package:learning_1ui_6228/views/nav_pages/profile_page.dart';
import 'package:http/http.dart' as http;

import '../model/UserModel.dart';

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

  @override
  State<FirstScreen> createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
  Future<UserModel>? futureUser;
  TextEditingController textController = TextEditingController();

  // List<UserModel> userList=[];
  @override
  void initState() {
    
    getData();
    super.initState();
  }

List<UserModel> userModelList=[];
Future<List<UserModel>> getData () async {
    final response=await http.get(Uri.parse('https://reqres.in/api/users?page=2'));
    var data = jsonDecode(response.body.toString());
    if(response.statusCode==200){
      for(Map i in data){
        userModelList.add(UserModel.fromJson(data[i]));
      }
      print('User Data list ${userModelList}');
      return userModelList;
    }else {
      return [];
    }

}



  @override
  Widget build(BuildContext context) {
    print('user list ModelListdata   $userModelList');
    return SafeArea(
      child: Scaffold(
        backgroundColor: Color(0xfff8f8fa),
        body: Column(
          children: [
            //1st Section
            Container(
              height: HelperClass.h250,
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: AppColors.gradientColor,
                  ),
                  borderRadius: BorderRadius.only(
                      bottomRight: Radius.circular(HelperClass.r10),
                      bottomLeft: Radius.circular(HelperClass.r10))),
              child: Column(
                children: <Widget>[
                  //Text and cross button
                  Container(
                    margin: EdgeInsets.only(
                        left: HelperClass.w10,
                        right: HelperClass.w10,
                        top: HelperClass.h20),
                    height: HelperClass.h50,
                    // color: Colors.red,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Container(
                            child: IconButton(
                                onPressed: () {},
                                icon: Icon(
                                  Icons.clear,
                                  color: Colors.white,
                                  size: 30,
                                ))),
                        Expanded(
                          child: Container(
                            margin: EdgeInsets.only(right: 30),
                            alignment: Alignment.center,
                            //  color: Colors.lightBlueAccent,
                            child: Text(
                              'Search',
                              style: TextStyle(
                                  fontSize: HelperClass.t25,
                                  fontWeight: FontWeight.bold,
                                  color: Colors.white),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: HelperClass.h25,
                  ),
                  //Search Bar
                  Container(
                    margin: EdgeInsets.only(
                        left: HelperClass.w10, right: HelperClass.w10),
                    //color: Colors.amber,
                    height: HelperClass.h70,
                    width: double.infinity,
                    child: TextField(
                      controller: textController,
                      onChanged: (name) {
                        setState(() {
                         // searchUser(name);
                        });
                      },
                      decoration: InputDecoration(
                        prefix: Icon(
                          Icons.search,
                          size: 26,
                        ),
                        suffix: IconButton(
                          onPressed: () {
                            setState(() {
                              textController.clear();
                              //searchedList = userList;
                            });
                          },
                          icon: Icon(
                            Icons.clear,
                            size: 26,
                          ),
                        ),
                        hintText: 'Search',
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(3),
                          borderSide: BorderSide.none,
                        ),
                        filled: true,
                        fillColor: Colors.white,
                      ),
                    ),
                  ),
                ],
              ),
            ),

            // List View
            Expanded(
              child: FutureBuilder(
                future: getData(),
                  builder: (context, snapshot){
                if(snapshot.hasData){
                  return ListView.builder(
                    itemCount: userModelList.length,
                    shrinkWrap: true,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: EdgeInsets.all(10),
                        child: Column(
                          children: [
                            InkWell(
                              onTap: () {
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder: (context) => ProfilePage(
                                       // userName:snapshot.data!.data![index].firstName??'',
                                        userName: userModelList[index].data![index].firstName??'',

                                        //followers: snapshot.data!.data![index].id.toString(),
                                        followers: userModelList[index].data![index].id.toString()??'N/A',

                                       // address: snapshot.data!.data![index].email.toString(),
                                        address: userModelList[index].data![index].email??'',

                                        //following: snapshot.data!.data![index].lastName.toString(),
                                        following: userModelList[index].data![index].lastName,
                                        //imageUrl: snapshot.data!.data![index].avatar.toString(),
                                        imageUrl: userModelList[index].data![index].avatar??'N/A',
                                      ),
                                    ));
                              },
                              child: ListTileWidgets(
                                following:userModelList[index].data![index].lastName,
                                address:  userModelList[index].data![index].email??'',
                                imageUrl:userModelList[index].data![index].avatar??'N/A',
                                name: userModelList[index].data![index].firstName??'',
                                followersCount:
                                'Followers: ${userModelList[index].data![index].lastName}',
                                iconWidget: Icon(
                                  Icons.person_add_alt_outlined,
                                  color: Colors.red,
                                  size: 25,
                                ),
                              ),
                            ),
                            AppLine(
                                paddingLeft: 10,
                                paddingRight: 10,
                                heightLine: 1,
                                lineColor: Colors.grey),
                          ],
                        ),
                      );
                    },
                  );
                }
                else if (snapshot.hasError) {
                  return Text('${snapshot.error}');
                }

                // By default, show a loading spinner.
                return Center(child: CircularProgressIndicator());
              }),
            ),
          ],
        ),
      ),
    );
  }
}

**This is my data model

import 'dart:convert';
UserModel userModelFromJson(String str) => UserModel.fromJson(json.decode(str));
String userModelToJson(UserModel data) => json.encode(data.toJson());
class UserModel {
  UserModel({
      this.page, 
      this.perPage, 
      this.total, 
      this.totalPages, 
      this.data, 
      this.support,});

  UserModel.fromJson(dynamic json) {
    page = json['page'];
    perPage = json['per_page'];
    total = json['total'];
    totalPages = json['total_pages'];
    if (json['data'] != null) {
      data = [];
      json['data'].forEach((v) {
        data?.add(Data.fromJson(v));
      });
    }
    support = json['support'] != null ? Support.fromJson(json['support']) : null;
  }
  int? page;
  int? perPage;
  int? total;
  int? totalPages;
  List<Data>? data;
  Support? support;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['page'] = page;
    map['per_page'] = perPage;
    map['total'] = total;
    map['total_pages'] = totalPages;
    if (data != null) {
      map['data'] = data?.map((v) => v.toJson()).toList();
    }
    if (support != null) {
      map['support'] = support?.toJson();
    }
    return map;
  }

}

Support supportFromJson(String str) => Support.fromJson(json.decode(str));
String supportToJson(Support data) => json.encode(data.toJson());
class Support {
  Support({
      this.url, 
      this.text,});

  Support.fromJson(dynamic json) {
    url = json['url'];
    text = json['text'];
  }
  String? url;
  String? text;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['url'] = url;
    map['text'] = text;
    return map;
  }

}

Data dataFromJson(String str) => Data.fromJson(json.decode(str));
String dataToJson(Data data) => json.encode(data.toJson());
class Data {
  Data({
      this.id, 
      this.email, 
      this.firstName, 
      this.lastName, 
      this.avatar,});

  Data.fromJson(dynamic json) {
    id = json['id'];
    email = json['email'];
    firstName = json['first_name'];
    lastName = json['last_name'];
    avatar = json['avatar'];
  }
  int? id;
  String? email;
  String? firstName;
  String? lastName;
  String? avatar;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['email'] = email;
    map['first_name'] = firstName;
    map['last_name'] = lastName;
    map['avatar'] = avatar;
    return map;
  }

}

This is my api call https://reqres.in/api/users?page=2

CodePudding user response:

The issue is you are reading a single map here.

Do it like

Future<List<UserModel>> getData() async {
  final response =
      await http.get(Uri.parse('https://reqres.in/api/users?page=2'));
 
  if (response.statusCode == 200) {
    var data = jsonDecode(response.body);
    userModelList.add(UserModel.fromJson(data));
    return userModelList;
  } else {
    return [];
  }
}

CodePudding user response:

Haven't tested it, but I think the problem is here:

Future<List<UserModel>> getData () async {
    final response=await http.get(Uri.parse('https://reqres.in/api/users?page=2'));
    var data = jsonDecode(response.body.toString());
    if(response.statusCode==200){
      for(Map i in data){
        userModelList.add(UserModel.fromJson(data[i]));
      }
      print('User Data list ${userModelList}');
      return userModelList;
    }else {
      return [];
    }

}

data is a map according to the response, which is not iterable. In other words, you cannot do for (var i in data) since only Iterables can be used in such a format. Instead, it looks like the entire data is ONE UserModel, and you should be able to just decode data itself as an UserModel. I didn't see multiple users in the API response.

  • Related