从firebase获取一些数据并使用模型对其进行解码时发生这种情况,方法如下:
UserModel? userModel;
void getUser() {
emit(GetUserLoadingsState());
FirebaseFirestore.instance.collection('users').doc(uId).get().then((value) {
userModel = UserModel.fromJson(value.data()!);
emit(GetUserSuccessState());
}).catchError((error) {
emit(GetUserErrorState(error.toString()));
});
}
调用方法
return BlocProvider(
create: (BuildContext context) => AppCubit()..getUser(),
child: BlocConsumer<AppCubit, AppStates>(
listener: (context, state) {},
builder: (context, state) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: lightTheme,
home: startWidget,
);
},
),
);
和消费者
BlocConsumer<AppCubit, AppStates>(
listener: (context, state) {},
builder: (context, state) {
var user = AppCubit.get(context).userModel!;