当前位置: 首页 > 知识库问答 >
问题:

类型_InternalLinkedHashMap不是类型列表的子类型

萧建木
2023-03-14

我是颤振初学者。我得到这样的JSON响应时出错。

{
    dataKey: [
      {
        key1: value1,
        key2: value2,
        key3: value3,
      } 
    ] 
}

我一直在论坛上寻找这个问题,我发现是这样的。

Map<String, dynamic> map = json.decode(response.body);
List<dynamic> data = map["dataKey"];
print(data[0]["name"]);

但我不懂如何编写函数。这是我的密码。

Future<Map> getData() async {
var response = await http.get(
    'https://api.batulimee.com/v1_ship/port_detail?port_id=${widget.list[widget.index] 
['port_id'].toString()}');
return json.decode(response.body) as Map<String, dynamic>;
}

FutureBuilder<Map>(
          future: getData(),
          builder: (context, snapshot) {
            if (snapshot.hasError) print(snapshot.error);
            return snapshot.hasData
                ? new Ports(
                    list: snapshot.data['data'],
                  )
                : new Container();
          },
        ),

class Ports extends StatelessWidget {
final List list;
Ports({this.list});

@override
Widget build(BuildContext context) {
return ListView.builder(
  scrollDirection: Axis.horizontal,
  shrinkWrap: true,
  itemCount: list.length,
  itemBuilder: (context, i) {
    return Card(
      child: Container(
        padding: EdgeInsets.all(8),
        color: Colors.red,
        child: Text(
          list[i]['port_website'].toString(),
          style: TextStyle(color: Colors.white, fontSize: 24),
        ),
      ),
    );
  },
);
}
}

还有像这样的错误。

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'

我很抱歉问了同样的问题,因为我不明白

共有1个答案

赏彭薄
2023-03-14
  //define your model in defineModel
  Map<int, defineModel> yourMap = {};

和获取方法:

      Future<void> getFavoriteList() async {
        try {
          final response = await http.get('url');
          final extractedData = json.decode(response.body);
          if (extractedData == null) {
            return;
          }
          final Map<int, defineModel> loadedProducts = {};
        
          final lastExtract = extractedData['dataKey'];
            
          lastExtract.forEach((prodData) {
            loadedProducts.putIfAbsent(
            //you Must Define A UniqueKey in here
              key,
              () => defineModel(
                key1: prodData['key1'],
                key2: prodData['key2'],
                key3: prodData['key3'],
              ),
            );
          });        
          yourMap = loadedProducts;
        } catch (error) {
          throw (error);
        }
      }
 类似资料: