英文:
flutter : type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'
问题
大家好。我是新手使用Flutter。我正在制作一个UI,在其中我想使用API来显示数据,但却遇到了以下错误...
我不知道为什么会显示这个错误。请指导我在这里做错了什么,以便我可以修复它。
谢谢您的帮助。
以下是我想在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 initState() {
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),
),
),
);
},
);
}
这是用于API的FarmerPreHarvestingGetHttp.dart文件的代码
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";
}
}
}
这是API的模型类,即ShubhChintakModel.dart文件。
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;
}
}
这是我迄今为止尝试的,但未能解决错误。
英文:
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.
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.
答案1
得分: 0
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) {
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";
}
}
英文:
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";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论