我正在嘗試將 API 連接到我的應用程式并遇到錯誤:_TypeError (type 'Null' is not a subtype of type 'String')
錯誤發生在一行:title: Text(snapshot.data?[i].hourly),
我以為我已經實作了 null 安全性來避免這個錯誤,但它似乎沒有奏效。我將不勝感激任何幫助!
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);
}
uj5u.com熱心網友回復:
根據api response(回應資料鏈接),您response data不是,array of json但您正在嘗試渲染json object as array并嘗試將 json 欄位決議為錯誤的方式,這就是錯誤發生的原因。
您可以按照下面的代碼spinet,可以看到我如何決議json,然后可以根據您的要求進行修改。
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<dynamic> 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);
return jsonData;
}
@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) {
var hourly = snapshot.data['hourly'];
return ListTile(
title: Text(
"time: ${hourly['time'][0]} - temperature_2m: ${hourly['temperature_2m'][0]}",
),
subtitle: Text(
"latitude: ${snapshot.data['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);
}
uj5u.com熱心網友回復:
改變兩件事
前
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);
}
下一個
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'));
return jsonDecode(response.body);
}
前
if (!snapshot.hasData)
下一個
if (snapshot.hasData)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/453294.html
