运行天气应用程序时出现此错误我正在使用 json 和 http 库来获取数据但我不知道为啥会出现此错误
Posted
技术标签:
【中文标题】运行天气应用程序时出现此错误我正在使用 json 和 http 库来获取数据但我不知道为啥会出现此错误【英文标题】:This error appears when running the weather app I am using json and http library to fetch data but I don't know why this error appears运行天气应用程序时出现此错误我正在使用 json 和 http 库来获取数据但我不知道为什么会出现此错误 【发布时间】:2021-01-15 16:15:36 【问题描述】:错误代码
E/flutter (25456): [错误:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:NoSuchMethodError:调用了方法“[]” 空值。 E/flutter (25456): 接收器: null E/flutter (25456): 试过 调用:E/flutter(25456):#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5) E/flutter (25456): #1 _WeatherState.fetchLocationUpDate。 (包:tast_project/screenes/screenweather.dart:31:27)E/flutter (25456): #2 状态.setState (包:flutter/src/widgets/framework.dart:1148:30) E/flutter (25456): #3 _WeatherState.fetchLocationUpDate (包:tast_project/screenes/screenweather.dart:30:5)E/flutter (25456): E/颤振 (25456): #4 _WeatherState.initState (package:tast_project/screenes/screenweather.dart:42:5) E/flutter (25456):#5 StatefulElement._firstBuild (包:flutter/src/widgets/framework.dart:4355:58) E/flutter (25456):#6 ComponentElement.mount (包:flutter/src/widgets/framework.dart:4201:5) E/flutter (25456): #7 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #8 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456):#9 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456): #10 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #11 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456):#12 ComponentElement.performRebuild (包:flutter/src/widgets/framework.dart:4243:16) E/flutter (25456):#13 元素重建 (包:flutter/src/widgets/framework.dart:3947:5) E/flutter (25456): #14 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (25456): #15 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (25456): #16 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456):#17 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456):#18 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456):#19 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #20 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456): #21 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456):#22 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #23 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456):#24 ComponentElement.performRebuild (包:flutter/src/widgets/framework.dart:4243:16) E/flutter (25456):#25 元素重建 (包:flutter/src/widgets/framework.dart:3947:5) E/flutter (25456): #26 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (25456): #27 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (25456):#28 ComponentElement.mount (包:flutter/src/widgets/framework.dart:4201:5) E/flutter (25456): #29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456): #30 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456):#31 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456):#32 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456):#33 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456):#34 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:5445:14) E/flutter (25456):#35 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456):#36 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456):#37 ComponentElement.performRebuild (包:flutter/src/widgets/framework.dart:4243:16) E/flutter (25456):#38 元素重建 (包:flutter/src/widgets/framework.dart:3947:5) E/flutter (25456): #39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (25456): #40 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (25456):#41 组件元素.mount (包:flutter/src/widgets/framework.dart:4201:5) E/flutter (25456): #42 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (25456):#43 元素.updateChild (包:flutter/src/widgets/framework.dart:2988:12) E/flutter (25456):#44 ComponentElement.performRebuild (包:flutter/src/widgets/framework.da
Flutter code weather
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Weather extends StatefulWidget
@override
_WeatherState createState() => _WeatherState();
const ipkay = '14f4090b86e2548527113ccac56a5ae4';
class _WeatherState extends State<Weather>
var temp = 0;
String city='';
//var id = 4099194;
String location ;
var textEditingController = TextEditingController();
fetchSearch(String input) async
http.Response response = await http.get(
"http://api.openweathermap.org/data/2.5/weather?units=metric&appid=14f4090b86e2548527113ccac56a5ae4&q=" + input);
var result = json.decode(response.body);
setState(()
location =result['name'];
);
fetchLocationUpDate()async
http.Response response = await http.get(
'http://api.openweathermap.org/data/2.5/weather?units=metric&appid=14f4090b86e2548527113ccac56a5ae4&q=$location');
var result = json.decode(response.body);
setState(()
temp =result['main']['temp'];
city =result['name'];
);
glo(String city)
fetchSearch(city);
fetchLocationUpDate();
@override
void initState()
super.initState();
fetchLocationUpDate();
@override
Widget build(BuildContext context)
Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: Colors.transparent,
body: SafeArea(
child: Container(
width: size.width,
height: size.height,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assetes/img/night.jpg',
),
fit: BoxFit.fill)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'$temp.toString() C°',
style: TextStyle(color: Colors.white, fontSize: 30),
),
Text(
'$city',
style: TextStyle(color: Colors.white, fontSize: 30),
),
Card(
child: TextField(
onSubmitted: (String city)
glo(city);
,
controller: textEditingController,
decoration: InputDecoration(
hintText: 'please entre city',
prefixIcon: Icon(Icons.search),
),
),
),
)
],
),
),
),
);
【问题讨论】:
【参考方案1】:您可以在下面复制粘贴运行完整代码
第 1 步:double temp = 0;
第 2 步:glo
需要 async
和 await
glo(String city) async
await fetchSearch(city);
await fetchLocationUpDate();
第 3 步:检查城市是否存在
if (result["cod"] == 200)
setState(()
location = result['name'];
print("locaton $location");
);
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Weather extends StatefulWidget
@override
_WeatherState createState() => _WeatherState();
const ipkay = '14f4090b86e2548527113ccac56a5ae4';
class _WeatherState extends State<Weather>
double temp = 0;
//var id = 4099194;
String location;
var textEditingController = TextEditingController();
fetchSearch(String input) async
http.Response response = await http.get(
"http://api.openweathermap.org/data/2.5/weather?units=metric&appid=14f4090b86e2548527113ccac56a5ae4&q=" +
input);
var result = json.decode(response.body);
print("fetchSearch $response.body");
if (result["cod"] == 200)
setState(()
location = result['name'];
print("locaton $location");
);
fetchLocationUpDate() async
print("location to string $location.toString()");
http.Response response = await http.get(
'http://api.openweathermap.org/data/2.5/weather?units=metric&appid=14f4090b86e2548527113ccac56a5ae4&q=' +
location.toString());
print("LocationUpdate $response.body");
var result = json.decode(response.body);
if (result["cod"] == 200)
setState(()
temp = result['main']['temp'];
);
glo(String city) async
await fetchSearch(city);
await fetchLocationUpDate();
@override
void initState()
super.initState();
//fetchLocationUpDate();
@override
Widget build(BuildContext context)
Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: Colors.transparent,
body: SafeArea(
child: Container(
width: size.width,
height: size.height,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
'https://picsum.photos/250?image=9',
),
fit: BoxFit.fill)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'$temp.toDouble() C°',
style: TextStyle(color: Colors.white, fontSize: 30),
),
Card(
child: TextField(
onSubmitted: (String city)
glo(city);
,
controller: textEditingController,
decoration: InputDecoration(
hintText: 'please entre city',
prefixIcon: Icon(Icons.search),
),
),
),
// RaisedButton(
// onPressed: ()
// glo();
// ,
// )
],
),
),
),
);
void main()
runApp(MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Weather(),
);
class MyHomePage extends StatefulWidget
MyHomePage(Key key, this.title) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
int _counter = 0;
void _incrementCounter()
setState(()
_counter++;
);
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
【讨论】:
以上是关于运行天气应用程序时出现此错误我正在使用 json 和 http 库来获取数据但我不知道为啥会出现此错误的主要内容,如果未能解决你的问题,请参考以下文章
在尝试解析 JSON 数据 W/System.err 时出现此错误:org.json.JSONException:对客户没有价值