错误:NoSuchMethodError:方法"toDouble"在null上被调用.接收方:null尝试调用:toDouble()

编程入门 行业动态 更新时间:2024-10-24 01:58:14
本文介绍了错误:NoSuchMethodError:方法"toDouble"在null上被调用.接收方:null尝试调用:toDouble()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用fromJson函数来解析json文件.但是在运行测试时,我收到此错误消息.

错误:NoSuchMethodError:方法'toDouble'在null上调用.接收者:null尝试调用:toDouble()

我不确定这是怎么回事.

我的代码

weather_model_app_test.dart

group('fromJson',(){测试(应返回有效模型",()异步{最终Map< String,dynamic>jsonMap =json.decode(fixture('weather_app.json'));//行为最终结果= WeatherAppModel.fromJson(jsonMap);//断言Expect(结果,tWeatherAppModel);});});

weather_app_model.dart

工厂WeatherAppModel.fromJson(Map< String,dynamic> json){返回WeatherAppModel(weatherMain:json ['weather'] [0] ['main'],weatherDescription:json ['weather'] [0] ['description'],temp:(json ['main'] ['temp']为double).toDouble(),minTemp:(json ['main'] ['temp_min']为double).toDouble(),maxTemp:(json ['main'] ['temp_main']为double).toDouble(),国家/地区:json ['sys'] ['country'],);}

fixtures/weather_app.dart

{"coord":{"lon":78,"lat":20},天气": [{"id":500,"main":雨","description":小雨","icon":"10d"}],"base":"model",主要的": {温度":301.51,压力":1014,湿度":67,"temp_min":301.51,"temp_max":301.51,海平面":1014,"grnd_level":979},风": {速度":3.19,度":77},雨": {"3小时":1.81},云":{全部":45},"dt":1577262029,"sys":{"country":"IN",日出":1572655775,日落":1572696807},时区":19800,"id":1272596,"name":"Digras",鳕鱼":200}

解决方案

不应 maxTemp:(json ['main'] ['temp_main'] as double).toDouble(),

在您的weather_app_model.dart文件中是 [temp_max] 而不是 [temp_main] ?

I am trying to make fromJson function which parse the json file. But while running test I am getting this error saying.

ERROR: NoSuchMethodError: The method 'toDouble' was called on null. Receiver: null Tried calling: toDouble()

I am not sure what is wrong with this.

My code

weather_model_app_test.dart

group('fromJson', () { test('should return a valid model', () async { final Map<String, dynamic> jsonMap = json.decode(fixture('weather_app.json')); //act final result = WeatherAppModel.fromJson(jsonMap); //assert expect(result, tWeatherAppModel); }); });

weather_app_model.dart

factory WeatherAppModel.fromJson(Map<String, dynamic> json) { return WeatherAppModel( weatherMain: json['weather'][0]['main'], weatherDescription: json['weather'][0]['description'], temp: (json['main']['temp'] as double).toDouble(), minTemp: (json['main']['temp_min'] as double).toDouble(), maxTemp: (json['main']['temp_main'] as double).toDouble(), country: json['sys']['country'], ); }

fixtures/weather_app.dart

{ "coord": { "lon": 78, "lat": 20 }, "weather": [ { "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" } ], "base": "model", "main": { "temp": 301.51, "pressure": 1014, "humidity": 67, "temp_min": 301.51, "temp_max": 301.51, "sea_level": 1014, "grnd_level": 979 }, "wind": { "speed": 3.19, "deg": 77 }, "rain": { "3h": 1.81 }, "clouds": { "all": 45 }, "dt": 1572672029, "sys": { "country": "IN", "sunrise": 1572655775, "sunset": 1572696807 }, "timezone": 19800, "id": 1272596, "name": "Digras", "cod": 200 }

解决方案

Shouldn't maxTemp: (json['main']['temp_main'] as double).toDouble(),

be [temp_max] instead of [temp_main] in your weather_app_model.dart file?

更多推荐

错误:NoSuchMethodError:方法"toDouble"在null上被调用.接收方:null尝试调用:toDouble()

本文发布于:2023-11-25 17:49:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   方法   NoSuchMethodError   null   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!