![](/img/trans.png)
[英]Error Occured: type 'Null' is not a subtype of type 'List<dynamic>' in type cast
[英]Api call throwing error type 'Null' is not a subtype of type 'List<Quotes>'
这是我的 api 调用。
class Httpservice {
final Uri postURL = Uri.parse(
"https://quotes.rest/qod?category=inspire",
);
static dynamic quoteOfTheDay;
Future<List<Quotes>> getQuotes() async {
Response res = await get(postURL);
final jsonItems = json.decode(res.body)["contents"]["quotes"];
List<Quotes> quote = jsonItems.map<Quotes>((json) {
return Quotes.fromJson(json);
}).toList();
quoteOfTheDay = quote;
return quote;
}
}
这是显示页面
class PostsPage extends StatelessWidget {
final List<Quotes> quote = Httpservice.quoteOfTheDay;
PostsPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("quotes"),
),
body: FutureBuilder(
//future: httpService.getQuotes(),
builder: (BuildContext context, AsyncSnapshot<List<Quotes>>
snapshot) {
if (snapshot.hasData) {
//Future.delayed(const Duration(seconds: 5));
// Quote? quotes = snapshot.data;
if (kDebugMode) {
print('" ${quote[0].quote} ?? '' "');
}
return Text('" ${quote[0].quote} ?? '' "');
} else {
return Scaffold(
body: Column(children: const [
SizedBox(height: 400.0),
Center(
child: CircularProgressIndicator(
color: Colors.black,
),
),
SizedBox(
height: 10.0,
),
Text('loading')
]),
);
}
},
),
);
}
}
这是模型数据
class Quotes {
String? quote;
String? length;
String? author;
List<String>? tags;
String? category;
String? language;
String? date;
String? permalink;
String? id;
String? background;
String? title;
Quotes(
{this.quote,
this.length,
this.author,
this.tags,
this.category,
this.language,
this.date,
this.permalink,
this.id,
this.background,
this.title});
Quotes.fromJson(Map<String, dynamic> json) {
quote = json['quote'];
length = json['length'];
author = json['author'];
tags = json['tags'].cast<String>();
category = json['category'];
language = json['language'];
date = json['date'];
permalink = json['permalink'];
id = json['id'];
background = json['background'];
title = json['title'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['quote'] = quote;
data['length'] = length;
data['author'] = author;
data['tags'] = tags;
data['category'] = category;
data['language'] = language;
data['date'] = date;
data['permalink'] = permalink;
data['id'] = id;
data['background'] = background;
data['title'] = title;
return data;
}
}
模型数据2
import 'quotes.dart';
class Contents {
List<Quotes>? quotes;
Contents({this.quotes});
Contents.fromJson(Map<String, dynamic> json) {
if (json['quotes']) {
quotes = <Quotes>[];
json['quotes'].forEach((v) {
quotes!.add(Quotes.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (quotes != null) {
data['quotes'] = quotes!.map((v) => v.toJson()).toList();
}
return data;
}
}
这是所有主要页面的代码。 当我尝试显示 Quote 类中的报价时,没有任何显示。 当我尝试导航到显示页面时,我不断收到此错误。引发了另一个异常:类型'Null'不是'List'类型的子类型我真的不知道我做错了什么并且我陷入了困境。
我认为问题出在这行final List<Quotes> quote = Httpservice.quoteOfTheDay;
. 由于变量quote
期望List<Quotes>
并且您正在调用返回空值的Httpservice.quoteOfTheDay
。
相反,您应该调用Httpservice.getQuotes()
,因为此方法返回预期值。
final List<Quotes> quote = Httpservice.getQuotes();
您正在使用 FutureBuilder 获取数据。 返回的快照确实具有列表,但是您在 FutureBuilder 之前获得了 quoteOfTheDay,它为空。 最好的方法是获取数据,当它被加载时,是通过快照。 这是工作代码:
帖子页面:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'http_service.dart';
import 'model_quote.dart';
class PostsPage extends StatelessWidget {
PostsPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("quotes"),
),
body: FutureBuilder(
future: Httpservice().getQuotes(),
builder: (BuildContext context, AsyncSnapshot<List<Quotes>> snapshot) {
if (snapshot.hasData) {
//Future.delayed(const Duration(seconds: 5));
// Quote? quotes = snapshot.data;
final List<Quotes> quotes = snapshot.data!;
return ListView.builder(
itemCount: quotes.length,
itemBuilder: (ctx, i) {
return Container(
child: Text('" ${quotes[0].quote} ?? ' ' "'));
});
} else {
return Column(children: const [
SizedBox(height: 400.0),
Center(
child: CircularProgressIndicator(
color: Colors.black,
),
),
SizedBox(
height: 10.0,
),
Text('loading')
]);
}
},
),
);
}
}
HTTP 服务
import 'dart:convert';
import 'package:http/http.dart';
import 'model_quote.dart';
class Httpservice {
final Uri postURL = Uri.parse(
"https://quotes.rest/qod?category=inspire",
);
Future<List<Quotes>> getQuotes() async {
Response res = await get(postURL);
final jsonItems = json.decode(res.body)["contents"]["quotes"];
List<Quotes> quote = jsonItems.map<Quotes>((json) {
print(json);
return Quotes.fromJson(json);
}).toList();
return quote;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.