【发布时间】:2021-11-24 04:34:46
【问题描述】:
我是新来的,我有静态数据,需要使用数据层“存储库和模型”编写干净的代码 在 initstate 中写入这一行之前,Loading 小部件只能工作 但是我发现我应该在initstate中写cubit dunction来发出loaded的状态, 请注意,我没有使用加载状态。只是初始和加载 有这个错误
TypeError: Cannot read properties of null (reading 'getAllPostsRep')
The relevant error-causing widget was
LayoutBuilder LayoutBuilder:file:///C:/Users/Michael/Desktop/task/New/lib/presentation/screens/home_screen.dart:34:24
When the exception was thrown, this was the stack
packages/facebookui/business_logic/cubit/posts_cubit.dart 15:38 getAllPosts
在initstate中写完这一行
class _FeedState extends State<Feed> {
List<PostModel> allPosts = [];
@override
void initState() {
super.initState();
BlocProvider.of<PostsCubit>(context).getAllPosts();
}
@override
Widget build(BuildContext context) {
return Expanded(
flex: 2,
child: Align(
alignment: AlignmentDirectional.topStart,
child: SingleChildScrollView(
child: Align(
alignment: AlignmentDirectional.topStart,
child: Column(
children: [
Stories(), //stories widget
Card(
child: Column(
children: [
WhatsOnYourMind(), //img,textfield of create post
CustomDivider(),
LiveSection(), //live, video and photos section
SizedBox(
height: 20,
)
],
),
),
//create room and rooms
Rooms(),
//all posts
BlocBuilder<PostsCubit, PostsState>(
builder: (context, state) {
if (state is PostsLoaded) {
setState(() {
allPosts = (state).postsList;
});
return Posts(allPosts: allPosts);
} else {
return Loading();
}
},
),
],
),
),
),
),
);
}
}
这是从 fly post 获取静态数据的存储库
class PostsRepository {
//charactersWebServices
Future<List<PostModel>> getAllPostsRep() async {
await Future.delayed(const Duration(seconds: 2));
//posts is a const data
return posts;
}
}
这是状态
part of 'posts_cubit.dart';
@immutable
abstract class PostsState {}
class PostsInitial extends PostsState {}
class PostsLoaded extends PostsState {
final List<PostModel> postsList;
PostsLoaded(this.postsList);
}
这是肘
class PostsCubit extends Cubit<PostsState> {
final PostsRepository postsRepository;
List<PostModel> allposts = [];
PostsCubit(this.postsRepository) : super(PostsInitial());
List<PostModel> getAllPosts() {
postsRepository.getAllPostsRep().then((value) {
emit(PostsLoaded(value));
allposts = value;
});
return allposts;
}
}
这是常量数据
List<PostModel> posts = [
PostModel(
name: 'Abdullah Ghayad',
time: 5,
text:
'The APIC Text is the most comprehensive and up-to-date reference for infection prevention and control (IPC). Written, edited, and reviewed by more than 200 subject matter experts, it reflects the latest guidelines, regulations, and standards of practice.The APIC Text\’s 11 sections and 125 peer-reviewed chapters are broad ranging, covering everything from fundamental principles, microbiology, epidemiology, and surveillance to more specialized topics, including specialty care populations, special pathogens, occupational health, and supportive care.',
comments: 5,
like: 50,
profileImage:
'http://c.files.bbci.co.uk/C870/production/_112921315_gettyimages-876284806.jpg',
images: [
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSCZlf5lc5tX-0gY-y94pGS0mQdL-D0lCH2OQ&usqp=CAU'
]),
]
【问题讨论】:
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。