所以,我可以接收資料并列印它,但它不會再正確地格式化到串列中。我附上了下面用于獲取資料和模型的所有代碼。加上列印輸出。有人能幫助我嗎。
獲取資料
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:world_time/services/model/article_model.dart';
class News {
String? city;
List? articles;
News({this.city});
Future<void> getNews() async {
try {
Response response = await get(Uri.parse('https://newsapi.org/v2/everything?q=$city&from=2021-12-05&sortBy=popularity&apiKey=MY API KEY'));
Map<String, dynamic> json = jsonDecode(response.body);
print(json);
List<dynamic> body = json['articles'];
articles = body.map((dynamic item) => Article.fromJson(item)).toList();
print(articles);
}
catch (e){
print("Caught error: $e");
}
}
}
列印(json)
{status: ok, totalResults: 520, articles: [{source: {id: bbc-news, name: BBC News}, author: null, title: Unstoppable Salah closes in on more records - his year in numbers, description: As Liverpool's Mohamed Salah closes in on another record, BBC Sport takes a statistical look at his superb 2021., url: https://www.bbc.co.uk/sport/football/59646200, urlToImage: https://ichef.bbci.co.uk/live-experience/cps/624/cpsprodpb/1269A/production/_122081457_mohamedsalah.jpg, publishedAt: 2021-12-16T08:49:23Z, content: Mohamed Salah has scored 36 goals in all competitions for Liverpool in 2021
I/flutter ( 2307): "He is phenomenal. For sure, at the moment, he is the best player in the world."
I/flutter ( 2307): Liverpool manager Jurgen Klopp may well … [ 5278 chars]}, {source: {id: reuters, name: Reuters}, author: null, title: Man Utd face Villa, Leicester host Watford in FA Cup third round - Reuters, description: Manchester United entertain Aston Villa in the FA Cup third round, while holders Leicester City welcome Watford and West Ham United are at home
列印(文章) 作為文章為空,catch 回傳此
I/flutter ( 2307): Caught error: type 'Null' is not a subtype of type 'String' in type cast
文章模型
class Article {
Source? source;
String? author;
String? title;
String? description;
String? url;
String? urlToImage;
String? publishedAt;
String? content;
Article(
{this.source,
this.author,
this.title,
this.description,
this.url,
this.urlToImage,
this.publishedAt,
this.content});
factory Article.fromJson(Map<String, dynamic> json) {
return Article(
source: Source.fromJson(json['source']),
author: json['author'] as String,
title: json['title'] as String,
description: json['description'] as String,
url: json['url'] as String,
urlToImage: json['urlToImage'] as String,
publishedAt: json['publishedAt'] as String,
content: json['content'] as String,
);
}
}
源模型
String? id;
String? name;
Source({this.id, this.name});
factory Source.fromJson(Map<String, dynamic> json) {
return Source(id: json['id'], name: json['name']);
}
}
uj5u.com熱心網友回復:
在您的模型上,您的所有變數都是型別String?,也就是說,變數將是String或null。但是當你創建模型時,你會像這樣分配所有變數:variable: json['variable'] as String,這意味著如果它們是,null它們將拋出錯誤,因為你做了as String,而不是as String?,這就是錯誤的來源。
為了解決這個問題,我相信應該可以完全洗掉as String,如下所示:
factory Article.fromJson(Map<String, dynamic> json) {
return Article(
source: Source.fromJson(json['source']),
author: json['author'],
title: json['title'],
description: json['description'],
url: json['url'],
urlToImage: json['urlToImage'],
publishedAt: json['publishedAt'],
content: json['content'],
);
}
上面的方法可能不起作用,如果它不起作用,以下應該:
factory Article.fromJson(Map<String, dynamic> json) {
return Article(
source: Source.fromJson(json['source']),
author: json['author'] as String?,
title: json['title'] as String?,
description: json['description'] as String?,
url: json['url'] as String?,
urlToImage: json['urlToImage'] as String?,
publishedAt: json['publishedAt'] as String?,
content: json['content'] as String?,
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/386111.html
