When I got this problem the issue was that the response I was receiving from the API didn't have the shape that my Dart model was expecting. Let me give you an example, and lets do it with your Shipping class. Say your query is returning a Json for a Shipping like so:
So following this example if I was to loop through the API response body, Dart would complain because I am accessing a 'key' instead of a 'Map'; and I think this is the issue you are facing. I tried to check against your API but the link was broken.
3条答案
按热度按时间wztqucjr1#
ShippingAddressService()是否具有默认构造函数以外的构造函数?如果是,请将其添加到代码示例中。**
您的代码:
我不止一次遇到了模拟问题。我的解决方案是将代码更改为:
一如既往YMMV
进一步查看您的代码,我的ShippingAddress可能是您的Shipping。
vqlkdk9b2#
你可以复制粘贴运行下面的完整代码
可以将返回类型定义为
getShippingAddressByUserId()
和_getShippingAddressByUserId()
您可以检查
Shipping
类的定义和FutureBuilder
的完整代码代码片段
工作演示
全码
vlju58qv3#
When I got this problem the issue was that the response I was receiving from the API didn't have the shape that my Dart model was expecting. Let me give you an example, and lets do it with your
Shipping
class.Say your query is returning a Json for a
Shipping
like so:Now this is not what you are expecting when you defined the model here:
What you are expecting is a response like this:
So following this example if I was to loop through the API response body, Dart would complain because I am accessing a 'key' instead of a 'Map'; and I think this is the issue you are facing. I tried to check against your API but the link was broken.