我收到此错误:在 get 方法 flutter 中,类型“String”不是类型“Null”的子类型
这是我使用的 get api 函数
Future<Stream<ServiceTypes>> getServiceTypesa() async {
final String url = 'https://test.com';
final client = new http.Client();
final streamedRest = await client.send(
http.Request('get', Uri.parse(url))
);
return streamedRest.stream
.transform(utf8.decoder)
.transform(json.decoder)
.expand((data) => (data as List))
.map((data) => ServiceTypes.fromJson(data));
}
你可以在这里看到该函数检测数据
我也看到了这个错误
StateError(错误状态:流已被监听。)
和这个错误
这是我的监听功能
@override
void initState() {
_serviceTypes = new List<ServiceTypes>();
listenForServiceTypes();
super.initState();
}
void listenForServiceTypes() async {
setState(() {
this._show_serviceTypesProgress = true;
});
final Stream<ServiceTypes> stream = await getServiceTypesa();
stream.listen((ServiceTypes serviceTypes) =>
setState(() => _serviceTypes.add(serviceTypes)));
setState(() {
this._show_serviceTypesProgress = false;
});
print(_serviceTypes);
}
这是我做的模型
I don't know what is the problem because the print function return : []
创建模型时,图像类型值为 null,因此模型是使用 Null 类型的图像创建的,但是当图像不是来自服务器的 null 类型时,它会给您一个错误
type 'String' is not a subtype of type 'Null' in get method flutter
因为您的图像类型是来自服务器的 String 并且您通过 Null 对象获取值,所以使用 String 类型对象来获取字符串值或图像。
Use this
Class ServiceTypes{
String image;
代替
Class ServiceTypes{
Null image;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)