问题
i want to get the content of the parsed json value that call URL
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<Photo>> fetchPhotos(http.Client client) async {
final response =
await client.get('https://pastebin.com/raw/RfNvKVPp');
return compute(parsePhotos, response.body);
}
List<Photo> parsePhotos(String responseBody) {
final parsed = jsonDecode(responseBody)['body'][0]['children'] as List;
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
}
class Photo {
final String URL;
final String text;
Photo({this.URL, this.text});
factory Photo.fromJson(Map<String, dynamic> json) {
return Photo(
text: json['text'] as String,
URL: json['URL'] as String,
);
}
}
void main() {
runApp(MyApp());}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appTitle = 'Isolate Demo';
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder<List<Photo>>(
future: fetchPhotos(http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? PhotosList(photos: snapshot.data)
: Center(child: CircularProgressIndicator());
},
),
);
}
}
class PhotosList extends StatelessWidget {
final List<Photo> photos;
PhotosList({Key key, this.photos}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: photos.length,
itemBuilder: (context, index) {
return ListTile(
title:Text(photos[index].URL, style: TextStyle(color: Colors.blueGrey, fontSize: 14),),
subtitle:Text(photos[index].text, style: TextStyle(color: Colors.red, fontSize: 14),),
leading: Icon(Icons.threed_rotation, color: Colors.pink,),
);
},
);
}
}
so as you can see, i could get the URL from json and i used it
title:Text(photos[index].URL, style: TextStyle(color: Colors.blueGrey, fontSize: 14),),
but what i want is, to get the content of the file that exist inside
photos[index].URL
we can do that by reading the response throw http or by getting the content directly by using httpclient.
http
Future<String> _initFuture(String url) async {
var resp = await http.get(url);
return resp.body;
}
httpclient
Future<String> ip(String url)
async {
HttpClient client = new HttpClient();
var request = await client.postUrl(Uri.parse(url));
var response = await request.close();
return response.single.then(utf8.decode);
}
so the problem is that i didn't know how to call the ip fuction or _initFuture function inside my code
to get the content of the url
title:Text(here i need the response or the content of the url not the url itself , style: TextStyle(color: Colors.blueGrey, fontSize: 14),),
instead of the url
title:Text(widget.photos[index].URL , style: TextStyle(color: Colors.blueGrey, fontSize: 14),),
thanks in advance
来源:https://stackoverflow.com/questions/64292444/including-the-content-of-the-url-inside-the-widget