在get方法flutter中,'String'类型不是'Null'类型的子类型 [英] type 'String' is not a subtype of type 'Null' in get method flutter

查看:44
本文介绍了在get方法flutter中,'String'类型不是'Null'类型的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:在 get 方法 flutter 中,类型 'String' 不是类型 'Null' 的子类型

I got this error: type 'String' is not a subtype of type 'Null' in get method flutter

这是我使用的 get api 函数

this is the get api function that i use

 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));
}

你可以在这里看到检测数据的功能

you can see here the function detect the data

我也看到了这个错误

StateError(错误状态:Stream 已被监听.)

StateError (Bad state: Stream has already been listened to.)

还有这个错误

这是我的监听功能

  @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 类型的图像创建的,但是当图像不是来自服务器的空类型时,它给你一个错误

When you create model, the image type value is null so, the model is create with Null type of image but when images are not null type from server then it gives you an error

     type 'String' is not a subtype of type 'Null' in get method flutter 

因为您的图像类型是来自服务器的字符串,并且您通过空对象获取值,所以使用字符串类型对象来获取字符串值或图像.

because your image type is String from server and you get value by Null Object, So use String type object for getting string value or images.

使用这个

  Class ServiceTypes{
  String image;

代替

   Class ServiceTypes{
   Null image;

这篇关于在get方法flutter中,'String'类型不是'Null'类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆