简体   繁体   English

我正在尝试使用从API获得的数据创建列表视图

[英]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.

相关问题 我正在尝试从数据库中获取数据,它返回给我这个 Future <List<dynamic> &gt; 我该如何迭代这个 - i am trying to get data from Database and it returns me this Future<List<dynamic>> how can i iterate this 为什么我在尝试创建列表时收到 _TypeError? - Why am I getting a _TypeError when trying to create a list? 我正在尝试借助颤振中的模型类从 api 加载数据 - I am trying to load data from api with the help of model class in flutter 我是 flutter 的新手,我正在尝试创建此布局 - I am new to flutter and i am trying to create this layout 我正在尝试创建将数字作为输入的 TextFormFields 列表,我想对所有这些数字求和 - I am trying to create list of TextFormFields which takes numbers as inputs and I want to Sum all those numbers 从 API 获取数据时出现错误,如果出现抖动,我应该如何获取数据? - I got an error while getting data from the API, how should I get the data if flutter? 我正在尝试在列表视图中显示我的 JSON 响应。 颤振/飞镖 - I am trying to display my JSON response in a List View. Flutter/Dart I am trying to fetch collection from shopify admin api with rest api in flutter - I am trying to fetch collection from shopify admin api with rest api in flutter 当我尝试在 listview.buider 中使用 snapshot.data.docs.length 时出现此错误:getter 'docs' 没有为类型 'Object' 定义 - i got this error when i am trying to use snapshot.data.docs.length in listview.buider: The getter 'docs' isn't defined for the type 'Object' 我正在尝试从 API 中获取 TimeZones,但由于某种原因,它没有返回所有 timeZones - I am trying to get TimeZones from an API in flutter but due to some reason its not returning all timeZones
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM