Dart 列表<;动态>';不是类型为';列表<;Obj>';内铸
我正在尝试创建动态下拉列表,它的值是使用micro服务从一个查找表填充的,但我在许多方面都尝试过相同的方法,直到现在我还没有成功地使其工作。因为我对飞镖/飞镖完全不熟悉 所以,任何人都可以请确定我在下面的代码中做错了什么 下面的代码用于调用Web服务Dart 列表<;动态>';不是类型为';列表<;Obj>';内铸,dart,flutter,flutter-dependencies,Dart,Flutter,Flutter Dependencies,我正在尝试创建动态下拉列表,它的值是使用micro服务从一个查找表填充的,但我在许多方面都尝试过相同的方法,直到现在我还没有成功地使其工作。因为我对飞镖/飞镖完全不熟悉 所以,任何人都可以请确定我在下面的代码中做错了什么 下面的代码用于调用Web服务 Future<List<CountryDropDownList>> getCountriesDetails(String url) async{ HttpClient httpClient = new HttpCli
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 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,),
],
);
},
);
}
}
Future getCountriesDetails(字符串url)异步{
HttpClient HttpClient=新HttpClient();
HttpClientRequest请求=等待httpClient.getUrl(Uri.parse(url));
//request.headers.set('content-type','application/json');
打印(“请求数据”+request.toString());
HttpClientResponse microServicesResponse=等待请求。关闭();
String microServicesResponseString=等待microServicesResponse.transform(utf8.decoder.join();
final parsed=wait json.decode(microservicessresponsestring.cast();
httpClient.close();
打印(“接收的数据”+microServicesResponseString.toString());
返回解析
.地图(
(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>,
);
}
}
classcountrydropdownlist{
字符串countryName;
列表状态下拉列表;
CountryDropDownList({this.countryName,this.stateDropDownList});
factory CountryDropDownList.fromJson(映射json){
返回国家下拉列表(
countryName:json['countryName']作为字符串,
stateDropDownList:json['states']作为列表,
);
}
}
仅用于显示当前正在运行的以下代码
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 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,),
],
);
},
);
}
}
class CenterFoundationSubmission扩展了StatefulWidget{
CenterFoundationSubmission({Key}):超级(Key:Key);
@凌驾
_CenterFoundationSubmissionState createState()=>新建CenterFoundationSubmissionState();
}
类_CenterFoundationSubmissionState扩展状态{
NetworkUtil _netUtil=新的NetworkUtil();
var url=“SomeUrl”;
@凌驾
void initState(){
super.initState();
设置状态(){
});
}
@凌驾
小部件构建(构建上下文){
var futureBuilder=新的futureBuilder(
未来:_getData(),
生成器:(BuildContext上下文,异步快照){
交换机(快照.连接状态){
案例连接状态。无:
案例连接状态。正在等待:
返回新文本('加载…');
违约:
if(snapshot.hasError)
返回新文本('此处的异常为:${snapshot.error}');
其他的
返回createView(上下文、快照);
}
},
);
归还新脚手架(
appBar:新的appBar(
标题:新文本(“中心基金会”),
),
正文:未来建设者,
);
}
Future\u getData()异步{
列表值=新列表();
addAll(_netUtil.getCountriesDetails(url)作为列表);
/*此处添加错误,将列表强制转换为对象类型时出错*/
等待新的未来。延迟(新的持续时间(秒:10));
返回值;
}
小部件createView(构建上下文上下文,异步快照快照){
列表值=snapshot.data;
返回新的ListView.builder(
itemCount:values.length,
itemBuilder:(构建上下文,int索引){
返回新列(
儿童:[
新ListTile(
标题:新文本(值[索引].countryName),
字幕:新文本(值[index].stateDropDownList[index].statesName),
尾随:新文本(值[index].stateDropDownList[index].districtDropDownList[index].districtsName),
),
新隔板(高度:2.0,),
],
);
},
);
}
}
我所做的就是尝试用多种方式调用webservices,尝试直接使用JSON字符串响应调用services响应,可能是我尝试过的事情
请在此提供帮助,我们将不胜感激。您能尝试一下
List.from()
构造函数吗?这里有一个相同的链接:
stateDropDownList:List.from(json['states']);
您能突出显示错误发生的那一行吗?完成只需使用他的“错误添加在此处”进行搜索,检查是否更改.toList();“to”.toList();”就可以解决问题。否,在运行时没有这样的方法尝试此方法:values.addAll(等待_netUtil.getCountriesDetails(url)作为列表);