flatterwordpress:getter&x27;长度';在null上调用了。接收方:null尝试调用:长度
我的颤振代码有一个小问题。我正在尝试使用flatter包“flatter\u wordpress”在我的应用程序中显示我网站上的帖子。如果我现在使用“https://demo.wp-api.org“正如示例代码中的网站一样,帖子显示正常。但是如果我在帖子中使用我自己的Wordpress网址,那么getter'length'的错误flatterwordpress:getter&x27;长度';在null上调用了。接收方:null尝试调用:长度,wordpress,flutter,Wordpress,Flutter,我的颤振代码有一个小问题。我正在尝试使用flatter包“flatter\u wordpress”在我的应用程序中显示我网站上的帖子。如果我现在使用“https://demo.wp-api.org“正如示例代码中的网站一样,帖子显示正常。但是如果我在帖子中使用我自己的Wordpress网址,那么getter'length'的错误被调用为null。接收方:空尝试呼叫:出现长度 import 'details_page.dart'; class LandingPage extends Statel
被调用为null。接收方:空尝试呼叫:出现长度
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),
],
)
],
),
),
),
),
);
},
);
},
),
),
);
}}
import'details_page.dart';
类LandingPage扩展了无状态小部件{
wp.WordPress-WordPress=wp.WordPress(
baseUrl:'https://pluto.faithlux.eu/wp-json/wp/v2/posts',
);
_启动URL(字符串链接)异步{
如果(等待canLaunch(链接)){
等待发射(链接);
}否则{
抛出“无法启动$link”;
}
}
_fetchPosts(){
Future posts=wordPress.fetchPosts(
后参数:wp.ParamsPostList(
上下文:wp.WordPressContext.view,
pageNum:1,
每页:5,
),
作者:是的,
fetchFeaturedMedia:true,
fetchComments:对
);
返回岗位;
}
_getPostImage(wp.Post-Post){
if(post.featuredMedia==null){
返回SizedBox();
}
返回Image.network(post.featuredMedia.sourceUrl);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
孩子:未来建设者(
未来:_fetchPosts(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.connectionState==connectionState.none){
返回容器();
}
if(snapshot.connectionState==connectionState.waiting){
返回中心(
子对象:CircularProgressIndicator(),
);
}
返回ListView.builder(
itemCount:snapshot.data.length±0,
itemBuilder:(上下文,索引){
wp.Post Post=snapshot.data[index];
回墨槽(
onTap:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>DetailsPage(post)
)
);
},
孩子:填充(
填充:常数边集全部(5.0),
孩子:卡片(
孩子:填充(
填充:常数边集全部(8.0),
子:列(
儿童:[
_getPostImage(post),
尺寸箱(高度:10,),
正文(
post.title.rendered.toString(),
样式:TextStyle(
fontWeight:fontWeight.bold,
尺寸:20
),
),
尺寸箱(高度:15,),
Html(
数据:post.extract.rendered.toString(),
onLinkTap:(字符串链接){
_启动URL(链接);
},
),
划船(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
文本(post.date.toString().replaceAll('T',''),
文本(post.author.name),
],
)
],
),
),
),
),
);
},
);
},
),
),
);
}}
站点上帖子的REST API是否按预期工作
这似乎是您网站的后端问题,而不是应用程序的问题。。此URL返回什么内容?:
为我工作:
Future\u fetchPosts()异步{
未来响应=wordPress.authenticateUser(
用户名:“您的管理员用户名”,
密码:“您的管理员密码”,
);
试一试{
wp.User=等待响应;
打印(“用户是:”);
打印(用户);
}捕获(e){
打印('未能获取用户:$e');
}
List posts=wait wordPress.fetchPosts(
后参数:wp.ParamsPostList(
上下文:wp.WordPressContext.view,
pageNum:1,
每页:10,
),
作者:是的,
fetchFeaturedMedia:true,
获取类别:true
);
返回岗位;
}
Hi:)谢谢你的快速回答!该页面实际上显示了一个错误。但我现在才注意到,因为链接昨天运行了很短时间。错误显示为:{“status”:“error”、“error”:“MISSING_AUTHORIZATION_HEADER”、“code”:“401”、“error_description”:“未收到授权头。授权头未发送或由于安全原因被服务器删除。”}
。你知道如何解决这个问题吗?哦,我明白了,我在上面的问题中发布了错误的代码。我将更改它。共享您的错误或错误截图
https://"yoursite".com/wp-json/wp/v2/posts
wp.WordPress wordPress = wp.WordPress(
baseUrl: 'https://your-website-domain-only',
authenticator: wp.WordPressAuthenticator.JWT,
adminName: '',
adminKey: '',
);
define('JWT_AUTH_SECRET_KEY', 'your-secret-key');
Future<List<wp.Post>> _fetchPosts() async {
Future<wp.User> response = wordPress.authenticateUser(
username: 'your admin username',
password: 'your admin password',
);
try {
wp.User user = await response;
print("User is: ");
print(user);
} catch (e) {
print('Failed to fetch user: $e');
}
List<wp.Post> posts = await wordPress.fetchPosts(
postParams: wp.ParamsPostList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 10,
),
fetchAuthor: true,
fetchFeaturedMedia: true,
fetchCategories: true
);
return posts;
}