ERROR: NoSuchMethodError: The method 'toDouble' was called on null. Receiver: null Tried calling: toDouble()
ERROR: NoSuchMethodError: The method 'toDouble' was called on null. Receiver: null Tried calling: toDouble()
我正在尝试创建解析 json 文件的 fromJson 函数。但是在 运行 测试时我收到这个错误提示。
ERROR: NoSuchMethodError: The method 'toDouble' was called on null.
Receiver: null
Tried calling: toDouble()
我不确定这有什么问题。
我的代码
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
}
不应该
maxTemp: (json['main']['temp_main'] as double).toDouble(),
在您的 weather_app_model.dart 文件中 [temp_max]
而不是 [temp_main]
?
import 'package:flutter/material.dart';
import 'components/body.dart';
import 'package:xplore/constants/size_config.dart';
class SignInScreen extends StatelessWidget {
static String routeName = "/login";
@override
Widget build(BuildContext context) {
**SizeConfig().init(context);**------> all this line
return Scaffold(
body: Body(),
);
}
}
主要问题是您没有提供 screenWidth 和 screenHeight 值。如果您提供它将解决您的问题。
当你的widget调用时它会调用realH但是screenWidth的值没有定义,所以它不能计算值也不能return值。
screenWidth = MediaQuery.of(context).size.width;
screenHeight = MediaQuery.of(context).size.height;
if (screenWidth > standardWidth) {
screenWidth = standardWidth;
}
if (screenHeight > standardHeight) {
screenHeight = standardHeight;
}
或者您可以制作一个单独的文件size_config.dart并向其中添加以下代码
import 'package:flutter/material.dart';
class SizeConfig {
static MediaQueryData _mediaQueryData;
static double screenWidth;
static double screenHeight;
static double defaultSize;
static Orientation orientation;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData.size.width;
screenHeight = _mediaQueryData.size.height;
orientation = _mediaQueryData.orientation;
}
}
// Get the proportionate height as per screen size
double getProportionateScreenHeight(double inputHeight) {
double screenHeight = SizeConfig.screenHeight;
// 812 is the layout height that designer use
return (inputHeight / 812.0) * screenHeight;
}
// Get the proportionate height as per screen size
double getProportionateScreenWidth(double inputWidth) {
double screenWidth = SizeConfig.screenWidth;
// 375 is the layout width that designer use
return (inputWidth / 375.0) * screenWidth;
}
并在 return 在屏幕上输入脚手架之前输入此代码
SizeConfig().init(context);
我正在尝试创建解析 json 文件的 fromJson 函数。但是在 运行 测试时我收到这个错误提示。
ERROR: NoSuchMethodError: The method 'toDouble' was called on null. Receiver: null Tried calling: toDouble()
我不确定这有什么问题。
我的代码
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
}
不应该
maxTemp: (json['main']['temp_main'] as double).toDouble(),
在您的 weather_app_model.dart 文件中 [temp_max]
而不是 [temp_main]
?
import 'package:flutter/material.dart';
import 'components/body.dart';
import 'package:xplore/constants/size_config.dart';
class SignInScreen extends StatelessWidget {
static String routeName = "/login";
@override
Widget build(BuildContext context) {
**SizeConfig().init(context);**------> all this line
return Scaffold(
body: Body(),
);
}
}
主要问题是您没有提供 screenWidth 和 screenHeight 值。如果您提供它将解决您的问题。
当你的widget调用时它会调用realH但是screenWidth的值没有定义,所以它不能计算值也不能return值。
screenWidth = MediaQuery.of(context).size.width;
screenHeight = MediaQuery.of(context).size.height;
if (screenWidth > standardWidth) {
screenWidth = standardWidth;
}
if (screenHeight > standardHeight) {
screenHeight = standardHeight;
}
或者您可以制作一个单独的文件size_config.dart并向其中添加以下代码
import 'package:flutter/material.dart';
class SizeConfig {
static MediaQueryData _mediaQueryData;
static double screenWidth;
static double screenHeight;
static double defaultSize;
static Orientation orientation;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData.size.width;
screenHeight = _mediaQueryData.size.height;
orientation = _mediaQueryData.orientation;
}
}
// Get the proportionate height as per screen size
double getProportionateScreenHeight(double inputHeight) {
double screenHeight = SizeConfig.screenHeight;
// 812 is the layout height that designer use
return (inputHeight / 812.0) * screenHeight;
}
// Get the proportionate height as per screen size
double getProportionateScreenWidth(double inputWidth) {
double screenWidth = SizeConfig.screenWidth;
// 375 is the layout width that designer use
return (inputWidth / 375.0) * screenWidth;
}
并在 return 在屏幕上输入脚手架之前输入此代码
SizeConfig().init(context);