我在从服务器API获取数据时遇到问题。结果返回空值。有人可以帮我解决吗?谢谢,非常感谢。
class TrackingPage extends StatefulWidget {
@override
_TrackingPageState createState() => _TrackingPageState();
}
class _TrackingPageState extends State<TrackingPage> {
_fetchPost() {
API.fetchPost().then((response) {
setState(() {
return Cargo.fromJson(response.body);
});
});
}
initState() {
super.initState();
_fetchPost();
}
dispose() {
super.dispose();
}
Widget _trackingpg() {
return Container(
child: FutureBuilder<Cargo>(
future: _fetchPost(),
builder: (context, snapshot) {
Text(snapshot.data.status);
},
));
}
}
这是我的API类
class API{
static Future fetchPost(){
var url = baseUrl;
return http.get(url);
}
}
PODO CLASS
Cargo cargoFromJson(String str) {
final jsonData = json.decode(str);
return Cargo.fromJson(jsonData);
}
String cargoToJson(Cargo data) {
final dyn = data.toJson();
return json.encode(dyn);
}
class Cargo {
String cargoClass;
String cargoId;
String type;
String status;
List<TemperatureReading> temperatureReadings;
Cargo({
this.cargoClass,
this.cargoId,
this.type,
this.status,
this.quantity,
this.origin,
this.temperatureReadings,
});
factory Cargo.fromJson(Map<String, dynamic> json) => new Cargo(
cargoClass: json["\u0024class"],
cargoId: json["cargoId"],
type: json["type"],
status: json["status"],
temperatureReadings: new List<TemperatureReading>.from(
json["temperatureReadings"]
.map((x) => TemperatureReading.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"\u0024class": cargoClass,
"cargoId": cargoId,
"type": type,
"status": status,
"temperatureReadings":
new List<dynamic>.from(temperatureReadings.map((x) => x.toJson())),
};
}
class TemperatureReading {
String temperatureReadingClass;
double centigrade;
String cargo;
String transactionTimestamp;
TemperatureReading({
this.temperatureReadingClass,
this.centigrade,
this.cargo,
this.transactionTimestamp,
});
factory TemperatureReading.fromJson(Map<String, dynamic> json) =>
new TemperatureReading(
temperatureReadingClass: json["\u0024class"],
centigrade: json["centigrade"].toDouble(),
cargo: json["cargo"],
transactionTimestamp: json["transactionTimestamp"],
);
Map<String, dynamic> toJson() => {
"\u0024class": temperatureReadingClass,
"centigrade": centigrade,
"cargo": cargo,
"transactionTimestamp": transactionTimestamp,
};
}
它显示“ getter状态”在null上被调用。 接收者:null” Json文件是嵌套列表格式。在这种情况下,我正在尝试获取字符串“状态”,但未成功。
答案 0 :(得分:0)
我做了这样的事情。如果可以的话,您可以尝试一下,这样会很棒。
Cargo parsePosts(String responseBody) {
var myClass = Cargo.fromJson(jsonDecode(responseBody));
for (var result in myClass.data.toList()) {
print('result');
print(result?.id);
}
return myClass;
}
Future<Cargo> fetchPosts(http.Client client) async {
final response = await client.get(
'base_url',
headers: {
"Authorization":
"if any"
});
print(response.body);
// compute function to run parsePosts in a separate isolate
return compute(parsePosts, response.body);
}
class HomePage extends StatelessWidget {
final String title;
HomePage({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: FutureBuilder<Cargo>(
future: fetchPosts(http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? ListViewPosts(posts: snapshot.data)
: Center(child: CircularProgressIndicator());
},
),
);
}
}
class ListViewPosts extends StatelessWidget {
final Cargo posts;
ListViewPosts({Key key, this.posts}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child:Text(posts.status)
);
}
}