List<dynamic>' 不是类型转换中类型 'List<Obj>' 的子类型
List<dynamic>' is not a subtype of type 'List<Obj>' in type cast
我正在尝试创建动态下拉列表,其值是使用微服务从其中一个查找 table 中填充的,但是我已经尝试了很多相同的方法,直到现在我还没有成功工作。因为我对 dart/flutter 完全陌生
所以任何人都可以确定我在下面的代码中做错了什么
以下代码用于调用webservice
Future<List<CountryDropDownList>> getCountriesDetails(String url) async{
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.getUrl(Uri.parse(url));
// request.headers.set('content-type', 'application/json');
print("request data "+request.toString());
HttpClientResponse microServicesResponse= await request.close();
String microServicesResponseString = await microServicesResponse.transform(utf8.decoder).join();
final parsed = await json.decode(microServicesResponseString).cast<Map<String, dynamic>>();
httpClient.close();
print("Data Recieved "+microServicesResponseString.toString());
return parsed
.map<CountryDropDownList>(
(json) => CountryDropDownList.fromJson(json))
.toList();
}
这是我的对象类型
class CountryDropDownList{
String countryName;
List<StateDropDownList> stateDropDownList;
CountryDropDownList({this.countryName, this.stateDropDownList});
factory CountryDropDownList.fromJson(Map<String, dynamic> json) {
return CountryDropDownList(
countryName: json['countryName'] as String,
stateDropDownList: json['states'] as List<StateDropDownList>,
);
}
}
并且仅用于显示 运行 下面的代码
class CenterFoundationSubmission extends StatefulWidget {
CenterFoundationSubmission({Key key}) : super(key: key);
@override
_CenterFoundationSubmissionState createState() => new _CenterFoundationSubmissionState();
}
class _CenterFoundationSubmissionState extends State<CenterFoundationSubmission> {
NetworkUtil _netUtil = new NetworkUtil();
var url = "SomeUrl";
@override
void initState() {
super.initState();
setState(() {
});
}
@override
Widget build(BuildContext context) {
var futureBuilder = new FutureBuilder(
future: _getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return new Text('loading...');
default:
if (snapshot.hasError)
return new Text('Exception here is : ${snapshot.error}');
else
return createView(context, snapshot);
}
},
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Center Foundation"),
),
body: futureBuilder,
);
}
Future<List<CountryDropDownList>> _getData() async {
List<CountryDropDownList> values = new List<CountryDropDownList>();
values.addAll(_netUtil.getCountriesDetails(url) as List<CountryDropDownList>);
/*Error Added here , i am getting error while casting list to my object type*/
await new Future.delayed(new Duration(seconds: 10));
return values;
}
Widget createView(BuildContext context, AsyncSnapshot snapshot) {
List<CountryDropDownList> values = snapshot.data;
return new ListView.builder(
itemCount: values.length,
itemBuilder: (BuildContext context, int index) {
return new Column(
children: <Widget>[
new ListTile(
title: new Text(values[index].countryName),
subtitle: new Text(values[index].stateDropDownList[index].statesName),
trailing: new Text(values[index].stateDropDownList[index].districtDropDownList[index].districtsName),
),
new Divider(height: 2.0,),
],
);
},
);
}
}
我做了什么我已经尝试以多种方式调用 web 服务来调用它,尝试使用 JSON 字符串响应直接转换服务响应,可能我已经尝试过。
请你帮忙,我们将不胜感激。
你能试试List.from()
构造器吗?这是一个link相同的:
https://api.dart.dev/stable/2.8.4/dart-core/List/List.from.html
stateDropDownList: List<StateDropDownList>.from(json['states']);
我正在尝试创建动态下拉列表,其值是使用微服务从其中一个查找 table 中填充的,但是我已经尝试了很多相同的方法,直到现在我还没有成功工作。因为我对 dart/flutter 完全陌生 所以任何人都可以确定我在下面的代码中做错了什么
以下代码用于调用webservice
Future<List<CountryDropDownList>> getCountriesDetails(String url) async{
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.getUrl(Uri.parse(url));
// request.headers.set('content-type', 'application/json');
print("request data "+request.toString());
HttpClientResponse microServicesResponse= await request.close();
String microServicesResponseString = await microServicesResponse.transform(utf8.decoder).join();
final parsed = await json.decode(microServicesResponseString).cast<Map<String, dynamic>>();
httpClient.close();
print("Data Recieved "+microServicesResponseString.toString());
return parsed
.map<CountryDropDownList>(
(json) => CountryDropDownList.fromJson(json))
.toList();
}
这是我的对象类型
class CountryDropDownList{
String countryName;
List<StateDropDownList> stateDropDownList;
CountryDropDownList({this.countryName, this.stateDropDownList});
factory CountryDropDownList.fromJson(Map<String, dynamic> json) {
return CountryDropDownList(
countryName: json['countryName'] as String,
stateDropDownList: json['states'] as List<StateDropDownList>,
);
}
}
并且仅用于显示 运行 下面的代码
class CenterFoundationSubmission extends StatefulWidget {
CenterFoundationSubmission({Key key}) : super(key: key);
@override
_CenterFoundationSubmissionState createState() => new _CenterFoundationSubmissionState();
}
class _CenterFoundationSubmissionState extends State<CenterFoundationSubmission> {
NetworkUtil _netUtil = new NetworkUtil();
var url = "SomeUrl";
@override
void initState() {
super.initState();
setState(() {
});
}
@override
Widget build(BuildContext context) {
var futureBuilder = new FutureBuilder(
future: _getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return new Text('loading...');
default:
if (snapshot.hasError)
return new Text('Exception here is : ${snapshot.error}');
else
return createView(context, snapshot);
}
},
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Center Foundation"),
),
body: futureBuilder,
);
}
Future<List<CountryDropDownList>> _getData() async {
List<CountryDropDownList> values = new List<CountryDropDownList>();
values.addAll(_netUtil.getCountriesDetails(url) as List<CountryDropDownList>);
/*Error Added here , i am getting error while casting list to my object type*/
await new Future.delayed(new Duration(seconds: 10));
return values;
}
Widget createView(BuildContext context, AsyncSnapshot snapshot) {
List<CountryDropDownList> values = snapshot.data;
return new ListView.builder(
itemCount: values.length,
itemBuilder: (BuildContext context, int index) {
return new Column(
children: <Widget>[
new ListTile(
title: new Text(values[index].countryName),
subtitle: new Text(values[index].stateDropDownList[index].statesName),
trailing: new Text(values[index].stateDropDownList[index].districtDropDownList[index].districtsName),
),
new Divider(height: 2.0,),
],
);
},
);
}
}
我做了什么我已经尝试以多种方式调用 web 服务来调用它,尝试使用 JSON 字符串响应直接转换服务响应,可能我已经尝试过。
请你帮忙,我们将不胜感激。
你能试试List.from()
构造器吗?这是一个link相同的:
https://api.dart.dev/stable/2.8.4/dart-core/List/List.from.html
stateDropDownList: List<StateDropDownList>.from(json['states']);