【发布时间】:2021-03-19 13:42:00
【问题描述】:
我在下面调用api:
{
"1": {
"data": {
"sensors": {
"fuel": [
{
"sensor_value": 2.9081154,
"sensor_unit": "m",
"percentage": 83.09,
"value": 48705.07,
"unit": "L",
"status": "normal",
"sensor": "fuel",
"name": "main_fuel"
}
],
"battery": [
{
"voltage": 4005,
"value": 100,
"status": "normal",
"unit": "%",
"sensor": "battery"
}
]
},
"latitude": null,
"longitude": null,
"speed": null,
"ignition": null,
"voltage": null,
"gsm": null,
"satellites": null,
"tracked_at": "2020-11-23 03:35:47",
"tracker": "jejaka",
"temperature": null
}
},
"2": {
"data": {
"sensors": {
"fuel": [
{
"sensor_value": 2.90697352,
"sensor_unit": "m",
"percentage": 83.06,
"value": 48687.99,
"unit": "L",
"status": "normal",
"sensor": "fuel",
"name": "main_fuel"
}
],
"battery": [
{
"voltage": 3901,
"value": 100,
"status": "normal",
"unit": "%",
"sensor": "battery"
}
]
},
"latitude": null,
"longitude": null,
"speed": null,
"ignition": null,
"voltage": null,
"gsm": null,
"satellites": null,
"tracked_at": "2020-11-23 03:44:02",
"tracker": "jejaka",
"temperature": null
}
}
}
这是我的 api 调用:
class Services{
// ignore: missing_return
static Future<Map<String, Tank>> fetchData() async {
String url = 'http://192.168.10.17/api/device';
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
final SharedPreferences prefs = await _prefs;
final token = prefs.getString('access_token');
final response = await http.get(url, headers: {
'Authorization': 'Bearer $token'
});
print('Token: $token');
if(response.statusCode == 200) {
final tank = tankFromJson(response.body);
return tank;
}else if(response.statusCode == 400) {
print('Connection to server is bad');
}else if(response.statusCode == 500){
print('No authorization');
}
}
}
此 api 需要用户令牌才能返回 api 数据如上。我已经得到了数据,但是现在当我实现数据时,出现以下错误:
The getter 'data' was called on null.
Receiver: null
Tried calling: data
这是我实现代码的方式。该 api 在列表视图中。我曾尝试将其放入未来的构建器中,但出现错误:
class TankCard extends StatefulWidget {
@override
_TankCardState createState() => _TankCardState();
}
class _TankCardState extends State<TankCard> {
final _tankRepository = Services();
Tank tank;
initState() {
Services.fetchData().then((tank){
setState((){
tank = tank;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return ListView.builder(
padding: EdgeInsets.all(0),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 2,
itemBuilder: (BuildContext context, int index){
return Column(
children: [
Container(
margin: EdgeInsets.only(top: 10),
width: MediaQuery.of(context).size.width * 1.1,
child: Card(
child: Column(
children: [
Container(
padding: EdgeInsets.only(top: 12),
child: Center(
child: Text('Tank 1 - Bukit Tinggi',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600
)),
)
),
Container(
padding: EdgeInsets.all(10),
child: SvgPicture.asset(
'assets/tank-icon-fill.svg',
// width: 50,
)
),
Container(
margin: EdgeInsets.fromLTRB(0, 5, 5, 0),
child: Column(
children: [
Wrap(
children: [
Icon(Icons.bolt),
Container(
margin: EdgeInsets.only(top: 4),
child: Text('${tank.data.sensors.battery[0].value.toString()}%')
)
],
),
Container(
margin: EdgeInsets.only(left: 15),
child: Text('Battery', style: TextStyle(
fontSize: 10,
fontWeight: FontWeight.w300,
),),
)
],
),
),
我从这个网址得到的坦克模型 -> https://app.quicktype.io/
任何帮助将不胜感激。
【问题讨论】:
标签: api flutter flutter-layout flutter-dependencies