json - Flutter 无法加载资产 json
问题描述
我通过调用另一个文件在 [] 中创建了卡片帖子,但是当我运行它时它没有显示数据。
class FeedScreen extends StatelessWidget {
final Future<List<Story>> futureListOfStory;
final Future<List<Post>> futureListOfPost;
const FeedScreen({
Key key,
@required this.futureListOfStory,
@required this.futureListOfPost,
}) : super(key: key);
@override
Widget build(BuildContext context) {
Size _screen = MediaQuery.of(context).size;
return Scaffold(
body: Container(
height: _screen.height,
width: _screen.width,
child: ListView.builder(
shrinkWrap: true,
itemCount: 2,
itemBuilder: (context, index) {
if (index == 0) {
return Container(
height: 98,
child: FutureBuilder(
future: futureListOfStory,
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Story> _storyList = snapshot.data;
return ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: _storyList.length,
itemBuilder: (context, index) {
return _storyAvatar(
height: 98,
width: 98,
story: _storyList.elementAt(index),
);
},
);
} else if (snapshot.hasError) {
print('error in Snapshot');
return Text('Error occured');
} else {
return CircularProgressIndicator();
}
},
),
);
}
return Container(
child: FutureBuilder(
future: futureListOfPost,
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Post> _postList = snapshot.data;
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: _postList.length,
itemBuilder: (context, index) {
return _postWidget(post: _postList.elementAt(index));
},
);
}
return CircularProgressIndicator();
},
),
);
},
),
),
);
}
Widget _postWidget({@required Post post}) {
return Container(
height: 500,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 1,
child: CircleAvatar(
backgroundImage: NetworkImage(post.profileImageUrl),
),
),
Expanded(
flex: 8,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text(
post.username,
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
)),
),
Expanded(
flex: 1,
child: Icon(
Feather.more_horizontal,
color: Colors.white,
),
),
],
),
),
),
Expanded(
flex: 6,
child: Container(
height: double.infinity,
width: double.infinity,
child: Image(
image: NetworkImage(post.postMediaUrl),
fit: BoxFit.cover,
),
),
),
Expanded(
flex: 1,
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Icon(
post.isLiked ? Icons.favorite : Icons.favorite_border,
color: Colors.white,
size: 28,
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Icon(
FontAwesome.comment_o,
color: Colors.white,
size: 28,
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Icon(
Feather.send,
color: Colors.white,
size: 28,
),
),
],
),
Icon(
post.isSaved
? FontAwesome.bookmark
: FontAwesome.bookmark_o,
color: Colors.white,
size: 28,
),
],
),
),
),
Expanded(
flex: 2,
child: Container(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('${post.likeCount} likes',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
)),
RichText(
text: TextSpan(
text: post.username,
style: TextStyle(
fontWeight: FontWeight.bold,
),
children: [
TextSpan(
text: ' ${post.postCaption}',
style: TextStyle(
fontWeight: FontWeight.normal,
),
),
],
),
),
Text(
'View all ${post.commentCount} comments',
style: TextStyle(
color: Colors.grey[500],
),
),
Text(
'${Utils.getTimeDifference(post.postCreationDate)}',
style: TextStyle(color: Colors.grey[500]),
)
],
),
),
),
),
],
),
);
}
Widget _storyAvatar({
@required double height,
@required double width,
@required Story story,
}) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 7.5),
height: height - 30, //155,
width: width - 30, //155,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: height - 30, //155,
width: width - 30, //155,
child: Center(
child: Stack(
children: [
Align(
alignment: Alignment.center,
child: Container(
height: height - 30, // 150,
width: width - 30, //150,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: story.isSeen ? Colors.grey[500] : Colors.red,
width: 3,
)),
),
),
Align(
alignment: Alignment.center,
child: Container(
height: height - 40, //140,
width: width - 40, //140,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: CircleAvatar(
backgroundImage: NetworkImage(story.imageUrl),
),
),
),
],
),
),
),
Text(
story.username,
style: TextStyle(color: Colors.white),
overflow: TextOverflow.ellipsis,
)
],
),
);
}
}
'''[VERBOSE-2:ui_dart_state.cc(177)] 未处理的异常:无法加载资产:assets/json/stories.json #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225: 7) #1 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:68:33) #2 CachingAssetBundle.loadString。(package:flutter/src/services/asset_bundle.dart:166:56) #3 _LinkedHashMapMixin.putIfAbsent (dart:collection-patch/compact_hash.dart:291:23) #4 CachingAssetBundle.loadString (package:flutter/src/services /asset_bundle.dart:166:27) #5 FeedRepo.getFutureListOfStoryFromJson (package:alpha/modules/home/repositories/feed_repo.dart:9:26) #6 _HomepageState.initState (package:alpha/pages/homeFeed.page.dart :25:35) #7 StatefulElement._firstBuild (包:flutter/src/widgets/framework.dart:4765:
{
"posts": [
{
"username": "Desuka00",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/1.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/1.jpeg',",
"isLiked": true,
"isSaved": false,
"likeCount": 1000,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 80,
"postCreationDate": 1591091160201
},
{
"username": "Desuka01",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/2.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/2.jpeg',",
"isLiked": false,
"isSaved": true,
"likeCount": 500,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 50,
"postCreationDate": 1591091160201
},
{
"username": "Desuka02",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/3.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/3.jpeg',",
"isLiked": true,
"isSaved": true,
"likeCount": 200,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 80,
"postCreationDate": 1591091160201
},
{
"username": "Desuka03",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/4.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/4.jpeg',",
"isLiked": false,
"isSaved": false,
"likeCount": 55,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 80,
"postCreationDate": 1591091160201
},
{
"username": "Desuka04",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/5.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/5.jpeg',",
"isLiked": true,
"isSaved": true,
"likeCount": 1,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 5,
"postCreationDate": 1591091160201
},
{
"username": "Desuka05",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/6.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/6.jpeg',",
"isLiked": true,
"isSaved": true,
"likeCount": 11,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 80,
"postCreationDate": 1591091160201
},
{
"username": "Desuka06",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/7.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/7.jpeg',",
"isLiked": false,
"isSaved": true,
"likeCount": 78,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 88,
"postCreationDate": 1591091160201
},
{
"username": "Desuka07",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/8.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/8.jpeg',",
"isLiked": true,
"isSaved": false,
"likeCount": 11,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 95,
"postCreationDate": 1591091160201
},
{
"username": "Desuka08",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/9.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/9.jpeg',",
"isLiked": true,
"isSaved": false,
"likeCount": 100,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 80,
"postCreationDate": 1591091160201
},
{
"username": "Desuka09",
"profileImageUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/10.jpeg',",
"postMediaUrl": "'https://gitlab.com/2Shours/alphapic/-/raw/master/11.jpeg',",
"isLiked": true,
"isSaved": false,
"likeCount": 100,
"postCaption": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"commentCount": 80,
"postCreationDate": 1591091160201
}
]
}
解决方案
推荐阅读
- r - 在 R ggalluvial 中使用冲积层可视化排名变化
- android - 具有 3 行的 TextView 具有不同的高度,具体取决于填充行的数量
- flutter - 使用 JsonSerialzable 时如何避免 Flutter 中的重复
- javascript - Flashing buttons timing
- reactjs - 如何使全尺寸图像像地图一样水平和垂直滚动
- java - 这些实例化之间有什么区别?
- serverless - 角色无效或无法在 sls deploy 上担任
- php - 如何在while循环之外获取while循环的值?
- ruby-on-rails - 活动管理员 - 在集合操作中使用控制器方法
- r - R bootstrapping 重采样,每个 id 有多个观察值,并返回重采样数据作为结果