【问题标题】:how to get value from the List of Map?如何从地图列表中获取价值?
【发布时间】:2022-08-17 20:42:48
【问题描述】:

如何在此类对象中获取“盘子”值?另外,我怎样才能在“固定”中获得价值,因为它是列表?

[
  {
    \"id\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\",
    \"car\": {
      \"id\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\",
      \"plate\": \"string\",
      \"fixations\": [
        \"string\"
      ]
    },
    \"fixed\": \"2022-08-17T09:55:33.709Z\",
    \"speed\": 0,
    \"latitude\": 0,
    \"longitude\": 0,
    \"upload\": true,
    \"image\": {
      \"id\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\",
      \"file\": \"string\"
    }
  }
]

我写这样的代码:

 body: FutureBuilder<List>(
        future: AllFixation().fetchAllFixation(),
        builder: (context, snapshot) {
          return ListView.builder(
              itemCount: snapshot.data?.length ?? 0,
              itemBuilder: (context, index) {
                return Text(snapshot.data?[index][\"upload\"].toString() ?? \'\');
              });
        },
      ),

    标签: dart


    【解决方案1】:

    对于这种情况,您应该编写一个实现 .fromMap() 方法的类来轻松处理数据。但是,如果你想使用“原始”数据,你应该小心这种结构。在您的情况下,您可以像这样获得“盘子”值:

    snapshot.data?[index]["car"]["plate"] as String;
    

    要获得固定,您应该遵循以下 sn-p:

    var myList = snapshot.data?[index]["car"]["fixations"].cast<String>();
    

    然后,您可以像往常一样使用myList List&lt;String&gt;

    我用.cast&lt;String&gt;()而不是as List&lt;String&gt;,因为在某些情况下,“as List&lt;String&gt;”会抛出像“List&lt;String&gt; is not subtype of List&lt;dynamic&gt;”这样的异常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-02
      • 2016-04-04
      • 2016-12-20
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 2011-05-10
      • 2020-08-11
      相关资源
      最近更新 更多