类型 'Null' 不是 'String' 类型的子类型 颤振



我试图从API ("https://api.exchangerate.host/latest")获得货币转换数据。

标题中的上述错误在屏幕上以文本形式显示。

我正在尝试将此消息作为输出。

如果您或您的公司使用这个项目或喜欢我们所做的,请考虑支持我们,以便我们能够继续维护和发展这个项目。

以后的个别货币汇率。

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<Album> fetchAlbum() async {
final response = await http.get(Uri.parse('https://api.exchangerate.host/latest'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return Album.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
class Album {
final String msg;
Album({
required this.msg,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
msg: json['msg'],
);
}
}
class CurrencyPage extends StatefulWidget {
const CurrencyPage({Key? key}) : super(key: key);
@override
_CurrencyPageState createState() => _CurrencyPageState();
}
class _CurrencyPageState extends State<CurrencyPage> {
late Future<Album> futureAlbum;
@override
void initState() {
super.initState();
futureAlbum = fetchAlbum();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Fetch Data Example'),
),
body: Center(
child: FutureBuilder<Album>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!.msg);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
),
),
),
);
}
}

任何帮助将不胜感激!

您使用的模型不正确。将return Album.fromJson(jsonDecode(response.body));改为return jsonDecode(response.body);,得到snapshot.data!['motd']等响应数据

代码:

Future<dynamic> fetchAlbum() async {
final response = await http.get(Uri.parse('https://api.exchangerate.host/latest'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
print(jsonDecode(response.body));
return jsonDecode(response.body);
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
class Album {
final String msg;
Album({
required this.msg,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
msg: json['msg'],
);
}
}
class CurrencyPage extends StatefulWidget {
const CurrencyPage({Key? key}) : super(key: key);
@override
_CurrencyPageState createState() => _CurrencyPageState();
}
class _CurrencyPageState extends State<CurrencyPage> {
late dynamic futureAlbum;
@override
void initState() {
super.initState();
futureAlbum = fetchAlbum();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Fetch Data Example'),
),
body: Center(
child: FutureBuilder<dynamic>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!['motd']['msg']);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
),
),
);
}
}

msgkey在motd对象内响应。因此,将fetchAlbum替换为:

Future<Album> fetchAlbum() async {
final response = await http.get(Uri.parse('https://api.exchangerate.host/latest'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
var body = jsonDecode(response.body);

return Album.fromJson(body["motd"]);
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}

相关内容

  • 没有找到相关文章

最新更新