gpt4 book ai didi

flutter - Flutter下拉列表: 'String'类型不是 'int'的 'index'类型的子类型

转载 作者:行者123 更新时间:2023-12-03 03:00:12 26 4
gpt4 key购买 nike

我试图在flutter中使用下拉列表,并且应该通过HTTP请求添加下拉列表的值。

这是HTTP的代码。

Future<String> getData() async {
try{
var deviceid = '123';
var dtgUid = '123';

var body = { "uid" : dtgUid, "deviceid": deviceid};

var response = await http.post(url, body: json.encode(body)).timeout(Duration(seconds: 15),
onTimeout: (){
// throw Exception();
_showSnackBar(context,'Some issue with connectivity. Can not connect to server.',Colors.redAccent);
//or you can also
return null;
});

if(response. statusCode == 200){
final datafromdb = getNewsTypeFromJson(response.body);
setState(() {
_inProcess = false;
if(datafromdb.content.length == null){
count = 0;
}else{
count = datafromdb.content.length;
}

if(datafromdb.content.length > 0 && datafromdb.content[0].tagname != 'Empty'){
for (var i in datafromdb.content) {
print(i.tagname);
data.add(i.tagname);
}
}else{
nodata = 'No Record Found';
_showSnackBar(context,nodata,Colors.redAccent);
}

});
}
}catch(e){
print("Exception Caught: $e");
}


}

这是JSONParse。
import 'dart:convert';

GetNewsType getNewsTypeFromJson(String str) => GetNewsType.fromJson(json.decode(str));

class GetNewsType {
GetNewsType({
this.content,
this.success,
});

List<Content> content;
bool success;

factory GetNewsType.fromJson(Map<String, dynamic> json) => GetNewsType(
content: List<Content>.from(json["content"].map((x) => Content.fromJson(x))),
success: json["success"],
);

}

class Content {
Content({
this.tagname,
});

String tagname;

factory Content.fromJson(Map<String, dynamic> json) => Content(
// tagname: json["tagname"],
tagname: json == null ? 'Empty' : json["tagname"]
);
}

这是下拉代码。
DropdownButton(
items: data.map((item) {
return DropdownMenuItem(
child: Text(item['tagname']),
value: item['tagname'].toString(),
);
}).toList(),
onChanged: (newVal) {
setState(() {
_mySelection = newVal;
});
},
value: _mySelection,
),

当我运行该应用程序时,它给出以下错误。我不明白。
type 'String' is not a subtype of type 'int' of 'index'

最佳答案

我还没有执行代码,但是错误消息和代码item['tagname']中的这一行看起来像是像List<String>这样的key正在访问map
由于在.map()中,您拥有单独的Content对象,因此请像.一样使用item.tagname

关于flutter - Flutter下拉列表: 'String'类型不是 'int'的 'index'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62356026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com