我使用颤振处理移动应用程序。在这里,我想在 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 自定义帖子类型如何提取。我刚开始学习颤振,谁能指出我正确的方向。这是我的代码,我没有收到任何错误。
我认为问题在于代码中的 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());
}
}
注意:上面的代码是这个问题的简单指南。不要将它用于其他豪猪。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句