【发布时间】:2023-04-03 01:55:01
【问题描述】:
我的 Flutter 代码有一个小问题。我正在尝试使用颤振包“flutter_wordpress”在应用程序中显示我网站上的帖子。如果我现在使用“https://demo.wp-api.org”作为示例代码中的网站,则帖子将正常显示。但是,如果我在帖子中使用自己的 Wordpress 网站地址,则会出现错误The getter 'length' was called on null. Receiver: null Tried calling: length
import 'details_page.dart';
class LandingPage extends StatelessWidget {
wp.WordPress wordPress = wp.WordPress(
baseUrl: 'https://pluto.faithlux.eu/wp-json/wp/v2/posts',
);
_launchUrl(String link) async {
if (await canLaunch(link)) {
await launch(link);
} else {
throw 'Cannot launch $link';
}
}
_fetchPosts() {
Future<List<wp.Post>> posts = wordPress.fetchPosts(
postParams: wp.ParamsPostList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 5,
),
fetchAuthor: true,
fetchFeaturedMedia: true,
fetchComments: true
);
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(
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 ?? 0,
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),
],
)
],
),
),
),
),
);
},
);
},
),
),
);
}}
【问题讨论】: