不确定为什么我收到错误“未处理的异常:类型 '(dynamic) => Null' 不是类型 '(String,dynamic) => void' of 'f' 的子类型”。一切似乎都正确完成,但仍然出现错误
Json link: https://androidapp.factory2homes.com/api/get-shipping-address-by-user-id/3
Run Code Online (Sandbox Code Playgroud)
class ShippingAddressPage extends StatefulWidget {
@override
_ShippingAddressPageState createState() => _ShippingAddressPageState();
}
class _ShippingAddressPageState extends State<ShippingAddressPage> {
List<Shipping> _shippingAddressList = List<Shipping>();
@override
void initState() {
super.initState();
_getShippingAddressByUserId();
}
_getShippingAddressByUserId() async {
SharedPreferences _prefs = await SharedPreferences.getInstance();
int _userId = _prefs.getInt('userId');
ShippingAddressService _shippingAddressService = ShippingAddressService();
var result = await _shippingAddressService.getShippingAddressByUserId(_userId);
var shipping = json.decode(result.body);
shipping.forEach((shipping){
var model = Shipping();
model.id = shipping['id'];
model.name = shipping['name'];
model.address = shipping['address'];
setState(() {
_shippingAddressList.add(model);
});
});
}
Run Code Online (Sandbox Code Playgroud)
E/flutter (26002): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type '(dynamic) => Null' is not a subtype of type '(String, dynamic) => void' of 'f'
E/flutter (26002): #0 _ShippingAddressPageState._getShippingAddressByUserId (package:factory2homes/screens/shipping_address_page.dart:32:13)
E/flutter (26002): <asynchronous suspension>
E/flutter (26002): #1 _ShippingAddressPageState.initState (package:factory2homes/screens/shipping_address_page.dart:22:4)
Run Code Online (Sandbox Code Playgroud)
class ShippingAddressService{
Repository _repository;
ShippingAddressService(){
_repository = Repository();
}
getShippingAddressByUserId(int userId) async {
return await _repository.httpGetById('get-shipping-address-by-user-id', userId);
}
}
Run Code Online (Sandbox Code Playgroud)
ShippingAddressService() 是否有默认构造函数以外的构造函数?如果是这样,请将其添加到您的代码示例中。**
你的代码:
getShippingAddressByUserId(int userId) async {
return await _repository.httpGetById('get-shipping-address-by-user-id', userId);
}
Run Code Online (Sandbox Code Playgroud)
我不止一次遇到过类似的问题。我的解决方案是将代码更改为:
ShippingAddress getShippingAddressByUserId(int userId) async {
ShippingAddress _shippingAddress = await _repository.httpGetById('get-shipping-address-by-user-id', userId);
return _shippingAddress;
}
Run Code Online (Sandbox Code Playgroud)
一如既往,YMMV。
进一步查看您的代码,我的 ShippingAddress 可能是您的 Shipping。
归档时间: |
|
查看次数: |
15129 次 |
最近记录: |