Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

everyone. I am new to flutter. I am making an UI where I want to display the data with the help of the api but get stuck on the following error…

This is the screenshot of the error display on UI

I don’t know why it is displaying. Please guide me what I’m doing wrong here so that I could fix it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks for the help.

Below is the code where i want to display the text on the UI

import 'package:flutter/material.dart';
import 'package:shubh_chintak/ShubhChintak/constants.dart';
import 'package:shubh_chintak/ShubhChintak/reusableWidget.dart';
import 'package:shubh_chintak/ShubhChintakGetAPI/ShubhChintakHttp/FarmerPreHarvestingGetHttp.dart';
import 'package:shubh_chintak/ShubhChintakGetAPI/ShubhChintakModelClasses/ShubhChintakModel.dart';

import 'farmerPostHarvestingCropDescription/farmerPostHarvestingCropDescription.dart';

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

  @override
  State<FarmerPostHarvesting> createState() => _FarmerPostHarvestingState();
}

class _FarmerPostHarvestingState extends State<FarmerPostHarvesting> {
  final FarmerPreHarvestingGetHttp _farmerPreHarvestingGetHttp =
      FarmerPreHarvestingGetHttp();

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<List<FarmerPreHarvestingModel>>(
        future: _farmerPreHarvestingGetHttp.getPreHarvestingData(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            List<FarmerPreHarvestingModel> preHarvestingData = snapshot.data!;
            return showPreHarvestingData(context, preHarvestingData);
          } else if (snapshot.hasError) {
            return Center(
              child: Text(
                snapshot.error.toString(),
              ),
            );
          } else {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }
}

ListView showPreHarvestingData(
    BuildContext context, List<FarmerPreHarvestingModel> preHarvestingData) {
  return ListView.builder(
    itemCount: preHarvestingData.length,
    itemBuilder: (context, index) {
      return SizedBox(
        height: MediaQuery.of(context).size.height * 0.13,
        child: Card(
          shadowColor: kPrimaryColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20),
            side: const BorderSide(color: kPrimaryColor),
          ),
          elevation: 5,
          semanticContainer: true,          
          child: ListTile(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) =>
                      const FarmerPostHarvestingCropDescription(),
                ),
              );
            },
            title: reusableText(
                "${preHarvestingData[index].cropName}", 20, TextAlign.start),
            subtitle: reusableText(
                "${preHarvestingData[index].cropArea}", 15, TextAlign.start),
            trailing: reusableText(
                "${preHarvestingData[index].cropInitialCosting}",
                20,
                TextAlign.end),
          ),
        ),
      );
    },
  );
}

Here is the FarmerPreHarvestingGetHttp.dart file code for the API

import 'dart:convert';

import 'package:http/http.dart';
import 'package:shubh_chintak/ShubhChintakGetAPI/ShubhChintakModelClasses/ShubhChintakModel.dart';

class FarmerPreHarvestingGetHttp {
  final String baseUrl =
      "http://shubhchintak.digicommunique.com/Registration.asmx/ShowGetData";

  Future<List<FarmerPreHarvestingModel>> getPreHarvestingData() async {
    Response res = await get(Uri.parse(baseUrl));
    if (res.statusCode == 200) {
      List<dynamic> data = jsonDecode(res.body);
      List<FarmerPreHarvestingModel> preHarvestingData = data
          .map((dynamic item) => FarmerPreHarvestingModel.fromJson(item))
          .toList();
      return preHarvestingData;
    } else {
      throw "Something went wrong";
    }
  }
}

Here is the Model class of the API i.e., ShubhChintakModel.dart file.

class FarmerPreHarvestingModel {
  int? cropId;
  String? cropName;
  String? cropArea;
  String? cropInitialCosting;

  FarmerPreHarvestingModel({
    this.cropId,
    this.cropName,
    this.cropArea,
    this.cropInitialCosting,
  });

  FarmerPreHarvestingModel.fromJson(Map<String, dynamic> json) {
    cropId = json['crop_id'];
    cropName = json['crop_name'];
    cropArea = json['crop_area'];
    cropInitialCosting = json['crop_initialCosting'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['crop_id'] = this.cropId;
    data['crop_name'] = this.cropName;
    data['crop_area'] = this.cropArea;
    data['crop_initialCosting'] = this.cropInitialCosting;
    return data;
  }
}

This is what i had try so far, but did not able to solve the error.

>Solution :

your response are a map, and the data that you need is a list with key data

your response:

{
  "result": "Success",
  "message": "Show All Data",
  "data": [
    {
      "crop_id": 46,
      "crop_name": "Rice",
      "crop_area": "343",
      "crop_initialCosting": "10000"
    }
  ]
}

you need to get the data and serialize to your model

Workaround:

Future<List<FarmerPreHarvestingModel>> getPreHarvestingData() async {
    Response res = await get(Uri.parse(baseUrl));
    if (res.statusCode == 200) {
/// update here >>>>>>>
      final resp = jsonDecode(res.body); // this is Map
      List<dynamic> data = resp["data"]; // this is List
      List<FarmerPreHarvestingModel> preHarvestingData = data
          .map((dynamic item) => FarmerPreHarvestingModel.fromJson(item))
          .toList();
      return preHarvestingData;
    } else {
      throw "Something went wrong";
    }
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading