我想创建一个Flutter应用程序,该应用程序可以从Wordpress(WP REST API)获取最新帖子,但是运行该设备后我的设备出现红屏。我对Flutter的经验还不是很丰富,所以我希望我能在这里得到一些帮助。因为我不能在此处发布整个错误日志,因为我需要写更多的东西,所以我选择了这两行可能很有趣:
I / flutter(18219):_ScaffoldLayout定制多子级布局委托忘记布置以下子级: I / flutter(18219):_ScaffoldSlot.body:RenderPhysicalModel#e87c7 NEEDS-LAYOUT NEEDS-PAINT
home_widget.dart
import 'news_widget.dart';
import 'package:flutter/material.dart';
import 'platzhalter_widget.dart';
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _HomeState();
}
}
class _HomeState extends State<Home> {
int _currentIndex = 0;
final List<Widget> _children = [
NewsWidget(),
PlatzhalterWidget(Colors.deepOrange),
PlatzhalterWidget(Colors.green),
];
news_widget.dart
class NewsWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return NewsWidgetState();
}
}
class NewsWidgetState extends State<NewsWidget> {
// Base URL for the wordpress site
final String url = "https://xxxxxxxxxx.xxxx.com/";
//api
final String api = "wp-json/wp/v2/posts?_embed";
// Empty list for the posts
List posts;
// Function to fetch list of posts
Future<String> getPosts() async {
var res = await http.get(Uri.encodeFull(url + api),
headers: {"Accept": "application/json"});
// fill the post list with results and update state
setState(() {
var resBody = json.decode(res.body);
posts = resBody;
});
return "Success!";
}
@override
void initState() {
super.initState();
this.getPosts();
}