![](/img/trans.png)
[英]How can I display WordPress posts on a sub domain not running wordpress?
[英]How can I display WordPress posts in Flutter app?
我正在尝试在我的 Flutter 应用程序中实现一个简单的讨论板。 为此,我想将 WordPress 博客站点连接到该应用程序。 我按照本教程进行了稍微编辑以获取我的代码:
import 'package:flutter/material.dart';
import 'package:flutter_wordpress/flutter_wordpress.dart' as wp;
import 'package:flutter_html/flutter_html.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:azingg_starter/MomTalk_Home/wp_details.dart';
class LandingPage extends StatelessWidget {
wp.WordPress wordPress = wp.WordPress(
baseUrl: 'https://example.com/',
);
_fetchPosts() {
Future<List<wp.Post>> posts = wordPress.fetchPosts(
postParams: wp.ParamsPostList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 10,
),
fetchAuthor: true,
fetchFeaturedMedia: true,
fetchComments: true);
if (posts == null) {
return posts;
}
}
_getPostImage(wp.Post post) {
if (post.featuredMedia == null) {
return SizedBox();
}
return Image.network(post.featuredMedia.sourceUrl);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('MomTalk'),
centerTitle: true,
),
body: Container(
child: FutureBuilder(
future: _fetchPosts(),
builder:
(BuildContext context, AsyncSnapshot<List<wp.Post>> snapshot) {
if (snapshot.connectionState == ConnectionState.none) {
return Container();
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
wp.Post post = snapshot.data[index];
return InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsPage(post)));
},
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
_getPostImage(post),
SizedBox(
height: 10,
),
Text(
post.title.rendered.toString(),
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20),
),
SizedBox(
height: 15,
),
Html(
data: post.excerpt.rendered.toString(),
onLinkTap: (String link) {
_launchUrl(link);
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(post.date.toString().replaceAll('T', ' ')),
Text(post.author.name),
],
)
],
),
),
),
),
);
},
);
},
),
),
);
}
}
_launchUrl(String link) async {
if (await canLaunch(link)) {
await launch(link);
} else {
throw 'Cannot launch $link';
}
}
以前,我收到一个关于“getter 'length' is called on null”的错误,所以我添加了“if (posts == null)”代码行来解决这个问题。 但是,现在我看到一个空白屏幕(即使我的网站上有帖子)。 关于为什么它似乎认为我的网站上没有帖子,或者为什么没有收到帖子的任何想法?
您对_fetchPosts
使用有很多问题。
首先,使其async
。 这将使在其中等待期货变得更加直观。
其次,等待wordPress.fetchPosts
。 现在你直接调用它只会返回一个期货列表,你不能直接使用期货来填充你的页面。
第三,你在这段代码中有一个错字:
if (posts == null) {
return posts;
}
这意味着你只在它为空时返回posts
。 当posts
不为null 时,该函数不会返回任何内容,而在Dart 中,如果函数没有返回任何其他内容,则它们将始终返回null。 因此,此函数将始终返回 null。 因此,您可以将==
更改为!=
,但这会引入冗余,因为您只会在posts
不为空时返回posts
,但该函数无论如何都会返回空。 因此,您可以完全取消 if 检查。 (这是假设您没有使用空安全,您的代码会建议您没有。)
考虑到这些因素,完整的函数应该是这样的:
Future<List<wp.Post>> _fetchPosts() async {
List<wp.Post> posts = await wordPress.fetchPosts(
postParams: wp.ParamsPostList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 10,
),
fetchAuthor: true,
fetchFeaturedMedia: true,
fetchComments: true,
);
return posts;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.