[英]I am trying to create a list view with the data that I got from API
class Search extends StatefulWidget {
int id;
Search([this.id]);
@override
_SearchState createState() => new _SearchState();
}
class _SearchState extends State<Search> {
@override
void initState() {
super.initState();
}
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
widget.id;
return new Scaffold(
appBar: new AppBar(
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.exit_to_app),
onPressed: _getTicketDetails
),
],
centerTitle: true,
title: new Text
("TicketsDetails", style: const TextStyle(
fontFamily: 'Poppins'
,),
),
),
);
}
_getTicketDetails() async {
print(widget.id);
var userDetails = {};
final response = await http.get(
"https....", headers: {
HttpHeaders.AUTHORIZATION: access_token
});
List returnTicketDetails = json.decode(response.body);
print(returnTicketDetails);
for (var i = 0; i < (returnTicketDetails?.length ?? 0); i++) {
final ticketresponse = await http.get(
"https:...
.toString()}", headers: {
HttpHeaders.AUTHORIZATION:
access_token
});
userDetails[returnTicketDetails[i]["user_id"]] =
json.decode(ticketresponse.body);
}
print(userDetails);
}
}
I would like to display in a Listview the index of my userDeatails, however for some reason the compiler does not recognise the userDetails, hence it highlight it as an error. 我想在Listview中显示我的userDeatails的索引,但是由于某些原因,编译器无法识别userDetails,因此将其突出显示为错误。 I have done this before, but I don't get why I am encountering this issue now. 我之前已经做过,但是我不明白为什么现在遇到这个问题。
At the moment when I run it only display the appBar 在我运行时,它仅显示appBar
As mentioned in the comments, your userDetails
variable is scoped inside the _getTicketDetails
method. 如注释中所述,您的userDetails
变量的作用域位于_getTicketDetails
方法内。 You need to declare it outside of that method if you want it visible to the rest of your class: 如果您希望它对您的其余部分可见,则需要在该方法之外声明它:
var userDetails = {}; // Moved outside
_getTicketDetails() async {
...
}
Though note that you should also call setState
when you modify this variable so that Flutter knows that this widget has changed and needs to be rebuild/rendered. 但是请注意,在修改此变量时也应该调用setState
,以便Flutter知道此小部件已更改,需要重新构建/呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.