我使用Flutter处理移动应用程序。在这里,我想访问flutter中的WordPress自定义帖子类型滑块。我试图喜欢这个
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() async {
List _jsonData = await getJson();
print(_jsonData);
runApp(new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Demo'),
centerTitle: true,
backgroundColor: Colors.redAccent,
),
body: new Column(
children: <Widget>[
new Text(_jsonData)
],
),
),
));
}
Future<List<Map<String,dynamic>>> getJson() async {
String apiUrl = 'http://bannermonster.com/demo.json';
http.Response response = await http.get(apiUrl);
return json.decode(response.body);
}
在这里,我的问题是我应该如何在抖动完整的滑块数据中提取WordPress自定义帖子类型,以及如何提取。我刚刚开始学习颤振,任何人都可以指出正确的方向。这是我的代码,没有任何错误。
答案 0 :(得分:1)
我认为问题出在代码中的URL拼写错误。但是,完整的示例在这里:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(new MaterialApp(
home: MainScreen(),
));
}
class MainScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getJson(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return Container(
child: new Scaffold(
appBar: new AppBar(
title: new Text('Demo'),
centerTitle: true,
backgroundColor: Colors.redAccent,
),
body: new Column(
children: <Widget>[new Text(snapshot.data[0]['id'].toString())],
),
),
);
} else if(snapshot.hasError) {
return Container(
child: new Scaffold(
appBar: new AppBar(
title: new Text('Demo'),
centerTitle: true,
backgroundColor: Colors.redAccent,
),
body: new Column(
children: <Widget>[new Text(snapshot.error.toString())], //Handle error in your own way
),
),
);
}else{
return Scaffold(
body: Container(
child: Center(child: CircularProgressIndicator()),
),
);
}
});
}
}
Future<List<dynamic>> getJson() async {
try {
String apiUrl = 'http://bannersmonster.com/demo.json';
http.Response response = await http.get(apiUrl);
return json.decode(response.body);
} catch (e) {
throw Exception('Problem with data'+e.toString());
}
}
注意:上面的代码是解决此问题的简单指南。不要将其用于其他海豚。