[英]Nasa API FLUTTER WITH pub.dev
| | I used to crate an App on flutter wiht the Nasa API but i only get strings back, but i wanna have pictures in my App how do i get picturers from this strings?
我曾经在 flutter 上用 Nasa API 创建一个应用程序,但我只得到字符串,但我想在我的应用程序中有图片我如何从这个字符串中获取图片? I already tried this but this but i dont finde the point where i coan change the program to give me the coice to macke a foto out of this String
我已经尝试过这个但是这个但是我没有找到我可以改变程序让我有机会从这个字符串中制作照片的地步
import 'package:flutter/material.dart';
import 'package:nasa_apis/nasa_apis.dart';
import 'package:tuple/tuple.dart';
class ApodScreen extends StatefulWidget {
const ApodScreen({super.key, required this.title});
final String title;
@override
State<ApodScreen> createState() => _ApodScreenState();
}
class _ApodScreenState extends State<ApodScreen> {
static const String _widgetNasaApod = "APOD";
String _selected = _widgetNasaApod;
String _apodTestDescription =
"The request will appear here.";
List<ApodItem> _apodTestResults = <ApodItem>[];
@override
void initState() {
super.initState();
init();
}
void init() async {
await Nasa.init(
logReceiver: (String msg, String name) {
},
apodSupport: true,
apodCacheSupport: true,
apodDefaultCacheExpiration: const Duration(seconds: 20));
}
@override
Widget build(BuildContext context) {
List<Widget> widgets = <Widget>[];
if (_selected == _widgetNasaApod) {
widgets.add(
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () async {
_apodTestResults.clear();
DateTime date = DateTime.now();
Tuple2<int, ApodItem?> result =
await NasaApod.requestByDate(date);
_apodTestDescription =
"requestByDate()\ndate[${date.toString()}]\nhttp response code: ${result.item1.toString()}";
if (result.item2 != null) {
_apodTestResults.add(result.item2!);
}
setState(() {});
},
child: const Text("Today"),
),
TextButton(
onPressed: () async {
_apodTestResults.clear();
Tuple2<int, List<ApodItem>?> result =
await NasaApod.requestByRandom(5);
_apodTestDescription =
"requestByRandom()\ncount[5]\nhttp response code: ${result.item1.toString()}";
if (result.item2 != null) {
_apodTestResults = result.item2!;
}
setState(() {});
},
child: const Text("Random"),
),
TextButton(
onPressed: () async {
_apodTestResults.clear();
DateTime startDate = DateTime(2022, 10, 1);
DateTime endDate = DateTime(2022, 10, 31);
Tuple2<int, List<MediaType>?> result =
(await NasaApod.requestByRange(startDate, endDate)) as Tuple2<int, List<MediaType>?>;
_apodTestDescription =
"requestByOctober()\n$startDate - $endDate\nhttp response code: ${result.item1.toString()}";
if (result.item2 != null) {
_apodTestResults = result.item2!.cast<ApodItem>();
}
setState(() {});
},
child: const Text("October"),
),
],
),
);
widgets.add(Text(_apodTestDescription));
for (ApodItem apodItem in _apodTestResults) {
widgets.add(
Padding(
padding: const EdgeInsets.all(15),
child: Text(
apodItem.toString(),
),
),
);
}
}
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: (){
Navigator.of(context).pop();
}, icon: const BackButtonIcon(),
),
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Column(
children: widgets,
),
),
);
}
}
Looking at the NASA API code , you can see the class ApodItem
definition ( Here ).查看NASA API 代码,您可以看到 class
ApodItem
定义( 此处)。 So in this part of your code:所以在你的这部分代码中:
for (ApodItem apodItem in _apodTestResults) {
widgets.add(
Padding(
padding: const EdgeInsets.all(15),
//Here you create a text widget
child: Text(
apodItem.toString(),
),
),
);
}
replace the Text widget by an Image.network using the getImageUrl
method of ApodItem
.使用ApodItem的
getImageUrl
方法将Text小部件替换为ApodItem
。 Like this:像这样:
for (ApodItem apodItem in _apodTestResults) {
widgets.add(
Padding(
padding: const EdgeInsets.all(15),
//Here you get the url from you APOD item and create an image widget
child: Image.network(
apodItem.getImageUrl(),
),
),
);
}
getImageUrl
will provide you with a url of the image (or of the thumbnail of the video if relevant. And Image.network will build the widget from the said url. getImageUrl
将为您提供图像的 url(或相关的视频缩略图) 。Image.network将从所述 url 构建小部件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.