type 'String' 不是 get 方法中类型 'Null' 的子类型 flutter
type 'String' is not a subtype of type 'Null' in get method flutter
我得到这个错误:type 'String' 不是 get 方法中类型 'Null' 的子类型 flutter
这是我使用的 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);
}
这是我做的模型
我不知道是什么问题,因为打印函数 return : []
问题很明显,您将 image
类型定义为 Null
然后尝试将字符串传递给它,尝试更改定义.. 像这样:
Class ServiceTypes{
int id;
String name;
String image;
...
当您创建模型时,图像类型值为 null,因此,模型是使用 Null 类型的图像创建的,但是当来自服务器的图像不是 null 类型时,它会给您一个错误
type 'String' is not a subtype of type 'Null' in get method flutter
因为你的图片类型是来自服务器的字符串,并且你通过空对象获取值,所以使用字符串类型的对象来获取字符串值或图片。
使用这个
Class ServiceTypes{
String image;
而不是
Class ServiceTypes{
Null image;
作为额外信息,您可以检查键名。我遇到了同样的问题,只是因为我试图获取一个找不到的密钥。
我第一次写 content
而不是 details
并且在我的 API
我有 details
而不是 content
.
祝你有美好的一天:)
使用 ?这对于可为空的键。例如:
最终字符串?标题;
最后的字符串?类别;
这是完整的解决方案:
Is it possible to parse json in Flutter/Dart with missing attributes (keys of json data)?
我得到这个错误:type 'String' 不是 get 方法中类型 'Null' 的子类型 flutter
这是我使用的 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);
}
这是我做的模型
问题很明显,您将 image
类型定义为 Null
然后尝试将字符串传递给它,尝试更改定义.. 像这样:
Class ServiceTypes{
int id;
String name;
String image;
...
当您创建模型时,图像类型值为 null,因此,模型是使用 Null 类型的图像创建的,但是当来自服务器的图像不是 null 类型时,它会给您一个错误
type 'String' is not a subtype of type 'Null' in get method flutter
因为你的图片类型是来自服务器的字符串,并且你通过空对象获取值,所以使用字符串类型的对象来获取字符串值或图片。
使用这个
Class ServiceTypes{
String image;
而不是
Class ServiceTypes{
Null image;
作为额外信息,您可以检查键名。我遇到了同样的问题,只是因为我试图获取一个找不到的密钥。
我第一次写 content
而不是 details
并且在我的 API
我有 details
而不是 content
.
祝你有美好的一天:)
使用 ?这对于可为空的键。例如:
最终字符串?标题; 最后的字符串?类别;
这是完整的解决方案: Is it possible to parse json in Flutter/Dart with missing attributes (keys of json data)?