问题描述
style.map("myButton.TButton",background=[('active','green')])
如您所见,我可以从json获取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,leading: Icon(Icons.threed_rotation,color: Colors.pink,);
},);
}
}
title:Text(photos[index].URL,
我们可以通过读取响应引发http或通过使用httpclient直接获取内容来实现。
http
photos[index].URL
httpclient
Future<String> _initFuture(String url) async {
var resp = await http.get(url);
return resp.body;
}
所以问题是我不知道如何在代码中调用ip函数或_initFuture函数
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);
}
代替网址
title:Text(here i need the response or the content of the url not the url itself,
预先感谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)