铸造模型不适用于 getX 颤振
Posted
技术标签:
【中文标题】铸造模型不适用于 getX 颤振【英文标题】:cast in model not working wit getX flutter 【发布时间】:2021-09-28 17:11:39 【问题描述】:我有一个来自 https://newsapi.org/ 的带有新闻 api 的应用程序
我的 quicktype 模型:
// To parse this JSON data, do
//
// final news = newsFromJson(jsonString);
import 'package:meta/meta.dart';
import 'dart:convert';
List<News> newsFromJson(String str) =>
List<News>.from(json.decode(str).map((x) => News.fromJson(x)));
String newsToJson(List<News> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class News
News(
required this.status,
required this.totalResults,
required this.articles,
);
final String status;
final int totalResults;
final List<Article> articles;
factory News.fromJson(Map<String, dynamic> json) => News(
status: json["status"],
totalResults: json["totalResults"],
articles: List<Article>.from(
json["articles"].map((x) => Article.fromJson(x))),
);
Map<String, dynamic> toJson() =>
"status": status,
"totalResults": totalResults,
"articles": List<dynamic>.from(articles.map((x) => x.toJson())),
;
class Article
Article(
required this.source,
required this.author,
required this.title,
required this.description,
required this.url,
required this.urlToImage,
required this.publishedAt,
required this.content,
);
final Source source;
final String author;
final String title;
final String description;
final String url;
final String urlToImage;
final DateTime publishedAt;
final String content;
factory Article.fromJson(Map<String, dynamic> json) => Article(
source: Source.fromJson(json["source"]),
author: json["author"] == null ? null : json["author"],
title: json["title"],
description: json["description"],
url: json["url"],
urlToImage: json["urlToImage"],
publishedAt: DateTime.parse(json["publishedAt"]),
content: json["content"],
);
Map<String, dynamic> toJson() =>
"source": source.toJson(),
"author": author == null ? null : author,
"title": title,
"description": description,
"url": url,
"urlToImage": urlToImage,
"publishedAt": publishedAt.toIso8601String(),
"content": content,
;
class Source
Source(
required this.id,
required this.name,
);
final String id;
final String name;
factory Source.fromJson(Map<String, dynamic> json) => Source(
id: json["id"] == null ? null : json["id"],
name: json["name"],
);
Map<String, dynamic> toJson() =>
"id": id == null ? null : id,
"name": name,
;
在我的 remoteservice.dart 中:
import 'package:http/http.dart' as http;
import 'package:nocovid/models/news.dart';
import 'package:nocovid/utils/constant.dart';
class RemoteServices
static var client = http.Client();
static Future<List<News>?> fetchNews() async
final String endpoint =
'https://newsapi.org/v2/everything?q=covid19&apiKey=' + kAPIKey;
final Uri url = Uri.parse(endpoint);
final response = await client.get(url);
if (response.statusCode == 200)
var jsonString = response.body;
return newsFromJson(jsonString);
else
return null;
newscontroller.dart
import 'package:get/state_manager.dart';
import 'package:nocovid/models/news.dart';
import 'package:nocovid/services/remote_services.dart';
class NewsController extends GetxController
var newsList = <News>[].obs;
@override
void onInit()
fetchNews();
super.onInit();
void fetchNews() async
var news = await RemoteServices.fetchNews();
if (news != null)
newsList.value = news;
并得到这个错误: 和
调用会定期执行,但在显示数据时会生成这些错误。 我检查了 github 上的一些代码,一切似乎都正常,但我无法继续
【问题讨论】:
【参考方案1】:改变
List<News> newsFromJson(String str) =>
List<News>.from(json.decode(str).map((x) => News.fromJson(x)));
到
News newsFromJson(String str) => News.fromJson(json.decode(str));
原因是 News 对象不是一个列表,它是一个复杂的 JSON,其中包含一个由文章列表组成的映射。您需要正确通过API。
如果你想要一个模型,你可以使用quicktype。只需粘贴 URL 响应即可。
也改变
static Future<List<News>?> fetchNews()
到
static Future<News> fetchNews()
【讨论】:
无法从方法“fetchNews”返回类型为“News”的值,因为它的返回类型为“Future?>” 我已经编辑了我的答案。以上是关于铸造模型不适用于 getX 颤振的主要内容,如果未能解决你的问题,请参考以下文章
Tensorflow - 多 GPU 不适用于模型(输入),也不适用于计算梯度