我在使用 Flutter 开发移动应用程序。这里我想在 flutter 中访问 WordPress 自定义帖子类型 slider 。我试着喜欢这个
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);
}
这里我的问题是我应该如何在 flutter complete slider 数据中拉取 WordPress 自定义帖子类型如何拉取。我刚开始学习 flutter,谁能给我指出正确的方向。这是我的代码,我没有收到任何错误。
最佳答案
我认为问题在于代码中 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());
}
}
注意:以上代码是针对此问题的简单指南。不要将它用于其他海豚。
关于android - 在 flutter 中解析来自 WordPress 自定义帖子类型的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51818226/