嗨,我正在開發我從課程中學到的天氣應用程式,一切正常,直到我開始重構,它變得復雜了代碼顯示沒有錯誤但是當我熱重啟應用程式時它崩潰顯示
The method '[]' was called on null.
Receiver: null
Tried calling: []("main")
在控制臺中
我什至不知道我的代碼哪里出了問題,我希望有人能在下面的代碼中學到很多東西以及我如何修復它: networking.dart
import 'package:http/http.dart' as http;
import 'dart:convert';
class NetworkHelper {
NetworkHelper(this.url);
final String url;
Future getData() async {
http.Response response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
String data = response.body;
var output = jsonDecode(data);
} else {
print(response.statusCode);
print('not working');
}
}
}
loading_screen.dart
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:clima/services/networking.dart';
import 'package:clima/screens/location_screen.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
const apiKey = '4c6ffd8e4e647128123739045f48d839';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
@override
double? latitude;
double? longtitude;
void initState() {
super.initState();
getLocationData();
}
void getLocationData() async {
Location location = Location();
await location.geolocation();
latitude = location.latitude;
longtitude = location.longtitude;
NetworkHelper networkHelper = NetworkHelper(
'https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longtitude&appid=$apiKey&units=metric');
var weatherdata = await networkHelper.getData();
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LocationScreen(
locationWeather: weatherdata,
);
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SpinKitDoubleBounce(
color: Colors.white,
size: 100.0,
),
),
);
}
}
location_screen.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:clima/utilities/constants.dart';
import 'package:clima/services/weather.dart';
class LocationScreen extends StatefulWidget {
final locationWeather;
LocationScreen({this.locationWeather});
@override
_LocationScreenState createState() => _LocationScreenState();
}
class _LocationScreenState extends State<LocationScreen> {
WeatherModel weather = WeatherModel();
int? temperature;
String? weatherIcon;
String? cityName;
String? weatherMessage;
@override
void initState() {
UpdateUi(widget.locationWeather);
super.initState();
}
void UpdateUi(dynamic weatherData) {
setState(() {
double temp = weatherData['main']['temp'];
temperature = temp.toInt();
var condition = weatherData['weather'][0]['id '];
cityName = weatherData['name'];
weatherMessage = weather.getMessage(temperature!);
weatherIcon = weather.getWeatherIcon(condition);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/location_background.jpg'),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.white.withOpacity(0.8), BlendMode.dstATop),
),
),
constraints: BoxConstraints.expand(),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlatButton(
onPressed: () {},
child: Icon(
Icons.near_me,
size: 50.0,
),
),
FlatButton(
onPressed: () {},
child: Icon(
Icons.location_city,
size: 50.0,
),
),
],
),
Padding(
padding: EdgeInsets.only(left: 15.0),
child: Row(
children: <Widget>[
Text(
weatherIcon!,
style: kTempTextStyle,
),
Text(
'??',
style: kConditionTextStyle,
),
],
),
),
Padding(
padding: EdgeInsets.only(right: 15.0),
child: Text(
"$weatherMessage in $cityName",
textAlign: TextAlign.right,
style: kMessageTextStyle,
),
),
],
),
),
),
);
}
}
這是 location_screen.dart 中的部分,我認為我犯了一個錯誤,我嘗試使用 jsonDecode(data) 來查看是否可以修復它,但它沒有,因為我之前已經在其他地方宣告過它:
void UpdateUi(dynamic weatherData) {
setState(() {
double temp = weatherData['main']['temp'];
temperature = temp.toInt();
var condition = weatherData['weather'][0]['id '];
cityName = weatherData['name'];
weatherMessage = weather.getMessage(temperature!);
weatherIcon = weather.getWeatherIcon(condition);
});
}
我嘗試修復它,但它沒有用,我實際上是 flutter 的初學者,我想這就是我有這個的原因,
所以我希望有人能引導我走上正確的道路……以防萬一我將來遇到這樣的情況,我可以自己解決,謝謝。![方法 '[]' 在 null 上被呼叫。接收器:null 嘗試呼叫:[](](https://img.uj5u.com/2022/01/03/0220be14cbde4c56b6ba5013a106ee5b.jpg)
uj5u.com熱心網友回復:
錯誤來自location_screen.dart第 27 行。我無法真正說出 API 請求發回的內容的性質。我可能是null,當你嘗試打電話時,weatherData['main']['temp']你遇到了錯誤。
檢查您的互聯網連接和終端是否有任何錯誤。你的實作似乎沒問題。
uj5u.com熱心網友回復:
嘗試洗掉idin后的空白空間var condition
uj5u.com熱心網友回復:
在networking.dart中,你必須像這樣回傳
String data = response.body;
var output = jsonDecode(data);
return output;
因為你在 loading_screen.dart 中使用了變數
var weatherdata = await networkHelper.getData();
var weatherdata 需要一個值,因此回傳 null
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/402068.html
下一篇:如何在顫振中從模態傳回資料?
