TypeError: Cannot read properties of null (reading 'getAllPostsRep') bloc cubit flutter
TypeError: Cannot read properties of null (reading 'getAllPostsRep') bloc cubit flutter
我是 bloc 的新手,我有静态数据,需要使用数据层“存储库和模型”编写干净的代码
在 initstate 中写入此行之前,加载小部件只能工作
但我发现我应该在 initstate 中写 cubit dunction 来发出加载状态,
请注意,我没有使用加载状态。只是初始和加载
有这个错误
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();
}
},
),
],
),
),
),
),
);
}
}
这是从 flie 获取静态数据的存储库 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'
]),
]
因为postsRepository为null,需要初始化
final PostsRepository postsRepository = PostsRepository();
并从构造函数中删除它
我是 bloc 的新手,我有静态数据,需要使用数据层“存储库和模型”编写干净的代码 在 initstate 中写入此行之前,加载小部件只能工作 但我发现我应该在 initstate 中写 cubit dunction 来发出加载状态, 请注意,我没有使用加载状态。只是初始和加载 有这个错误
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();
}
},
),
],
),
),
),
),
);
}
}
这是从 flie 获取静态数据的存储库 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'
]),
]
因为postsRepository为null,需要初始化
final PostsRepository postsRepository = PostsRepository();
并从构造函数中删除它