【问题标题】:type 'String' is not a subtype of type 'Null' in get method flutter类型'String'不是get方法颤动中类型'Null'的子类型
【发布时间】:2020-05-14 05:44:36
【问题描述】:

我得到了这个错误:类型'String'不是get方法flutter中'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(错误状态:Stream 已被监听。)

还有这个错误

这是我的监听功能

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

这是我做的模型

我不知道是什么问题,因为打印函数返回:[]

【问题讨论】:

  • 你确定'deleted_at'在你的api服务器的json里面吗?
  • 是的,我确定并添加模型以查看它

标签: api flutter get flutter-dependencies


【解决方案1】:

问题很清楚,您将image 类型定义为Null 然后尝试将字符串传递给它,尝试更改定义.. 像这样:

Class ServiceTypes{
  int id;
  String name;
  String image;
...

【讨论】:

    【解决方案2】:

    当您创建模型时,图像类型值为 null,因此,模型是使用 Null 类型的图像创建的,但是当图像不是来自服务器的 null 类型时,它会给您一个错误

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

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

    使用这个

      Class ServiceTypes{
      String image;
    

    而不是

       Class ServiceTypes{
       Null image;
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 2021-02-25
      • 1970-01-01
      • 2022-08-18
      • 2023-03-03
      相关资源
      最近更新 更多