_TypeError(类型“Null”不是类型“String”的子类型)-flutter 中发生错误
我正在尝试将API连接到我的应用程序,并遇到了错误:_typeerror(type'null'不是类型'string'的子类型)
错误出现在行上:title:titter:text:snapshot.data?[i? ]。小时),
我认为我已经实施了无效的安全性来避免此错误,但似乎没有起作用。感谢任何帮助!
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
));
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Future getUserData() async {
var response = await http.get(Uri.parse(
'https://api.open-meteo.com/v1/forecast? latitude=52.52&longitude=13.41&hourly=temperature_2m'));
var jsonData = jsonDecode(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Rangers Tool'),
),
body: Center(
child: Card(
child: FutureBuilder(
future: getUserData(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data?.length,
itemBuilder: (context, i) {
return ListTile(
title: Text(snapshot.data?[i].hourly),
subtitle: Text(snapshot.data[i]?.latitude),
);
});
} else {
return const Center(child: CircularProgressIndicator());
}
},
))),
);
}
}
getUserData() {}
class User {
final String? hourly,
time,
temperature_2m,
longitude,
generationtime_m,
hourly_units,
latitude;
User(this.longitude, this.latitude, this.hourly, this.time,
this.temperature_2m, this.generationtime_m, this.hourly_units);
}
I am trying to connect an API to my application and have come across the error: _TypeError (type 'Null' is not a subtype of type 'String')
The error occurs on the line: title: Text(snapshot.data?[i].hourly),
I thought that I had implemented null safety to avoid this error but it doesn't seem to have worked. I would appreciate any help!
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MaterialApp(
home: HomePage(),
debugShowCheckedModeBanner: false,
));
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Future getUserData() async {
var response = await http.get(Uri.parse(
'https://api.open-meteo.com/v1/forecast? latitude=52.52&longitude=13.41&hourly=temperature_2m'));
var jsonData = jsonDecode(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Rangers Tool'),
),
body: Center(
child: Card(
child: FutureBuilder(
future: getUserData(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data?.length,
itemBuilder: (context, i) {
return ListTile(
title: Text(snapshot.data?[i].hourly),
subtitle: Text(snapshot.data[i]?.latitude),
);
});
} else {
return const Center(child: CircularProgressIndicator());
}
},
))),
);
}
}
getUserData() {}
class User {
final String? hourly,
time,
temperature_2m,
longitude,
generationtime_m,
hourly_units,
latitude;
User(this.longitude, this.latitude, this.hourly, this.time,
this.temperature_2m, this.generationtime_m, this.hourly_units);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据
api 响应
(响应数据链接),您的响应数据
不是json 数组
,但您尝试将json 对象呈现为数组
并且还尝试以错误的方式解析 json 字段,这就是错误的原因发生。您可以按照下面的代码 spinet 来查看我如何解析 json,然后可以根据您的要求进行修改。
According to the
api response
(response data link), yourresponse data
is not anarray of json
but you are trying to render thejson object as array
and also trying to parse the json field as wrong way thats why the error occurred.You can follow the bellow code spinet and can see that how I can parse the json then can modify based on your requirement.
更改两
在下
之前
一个
件事
Change two things
Before
Next
before
next