I am trying to get data from FireStore it works fine after I restart the app but it shows this error for a second and brings to me the data, after first register or sign in it shows the below error till I restart the app:
The following NoSuchMethodError was thrown building BlocBuilder<AppCubit, AppStates>(dirty, state: _BlocBuilderBaseState<AppCubit, AppStates>#783ea):
The getter 'cover' was called on null.
Receiver: null
Tried calling: cover
I tried to add a circular indicator when the receiver is null like the below code:
image: userModel.cover == null
? CircularProgressIndicator()
: DecorationImage(
image: NetworkImage(
'${userModel.cover}',
),
fit: BoxFit.cover,
),
and this is the below getUserData method:
UserModel model;
void getUserData() {
emit(AppGetUserLoadingState());
FirebaseFirestore.instance.collection('users').doc(uId).get().then((value) {
// print(value.data());
model = UserModel.fromJson(value.data());
emit(
AppGetUserSuccessState(),
);
}).catchError((error) {
print(error.toString());
emit(
AppGetUserErrorState(
error.toString(),
),
);
});
}
and this is the below model I have:
class UserModel {
String name;
String email;
String phone;
String uId;
String image;
String cover;
String bio;
bool isEmailVerified;
UserModel({
this.name,
this.email,
this.phone,
this.uId,
this.image,
this.cover,
this.bio,
this.isEmailVerified,
});
UserModel.fromJson(Map<String, dynamic> json) {
email = json['email'];
name = json['name'];
phone = json['phone'];
uId = json['uId'];
image = json['image'];
cover = json['cover'];
bio = json['bio'];
isEmailVerified = json['isEmailVerified'];
}
Map<String, dynamic> toMap() {
return {
'name': name,
'email': email,
'phone': phone,
'uId': uId,
'image': image,
'cover': cover,
'bio': bio,
'isEmailVerified': isEmailVerified,
};
}
}
but still get the same error..
this is the below full code:
class SettingsScreen extends StatelessWidget {
const SettingsScreen({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocConsumer<AppCubit, AppStates>(
listener: (context, state) {},
builder: (context, state) {
var userModel = AppCubit.get(context).model;
// print(userModel.cover);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Container(
height: 190.0,
child: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
Align(
alignment: AlignmentDirectional.topCenter,
child: Container(
width: double.infinity,
height: 140.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
image: userModel.cover == null
? CircularProgressIndicator()
: DecorationImage(
image: NetworkImage(
'${userModel.cover}',
),
fit: BoxFit.cover,
),
),
),
),
CircleAvatar(
radius: 51.0,
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
child: CircleAvatar(
// radius: responsive.height * 0.025,
radius: 50.0,
backgroundImage: NetworkImage(
'${userModel.image}',
),
),
),
],
),
),
SizedBox(
height: 5.0,
),
Text(
'${userModel.name}',
style: Theme.of(context).textTheme.bodyText1,
),
Text(
'${userModel.bio}',
style: Theme.of(context).textTheme.caption,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20.0),
child: Row(
children: [
Expanded(
child: InkWell(
child: Column(
children: [
Text(
'100',
style: Theme.of(context).textTheme.subtitle2,
),
Text(
'Posts',
style: Theme.of(context).textTheme.caption,
),
],
),
onTap: () {},
),
),
Expanded(
child: InkWell(
child: Column(
children: [
Text(
'100',
style: Theme.of(context).textTheme.subtitle2,
),
Text(
'Posts',
style: Theme.of(context).textTheme.caption,
),
],
),
onTap: () {},
),
),
Expanded(
child: InkWell(
child: Column(
children: [
Text(
'62',
style: Theme.of(context).textTheme.subtitle2,
),
Text(
'Photos',
style: Theme.of(context).textTheme.caption,
),
],
),
onTap: () {},
),
),
Expanded(
child: InkWell(
child: Column(
children: [
Text(
'200',
style: Theme.of(context).textTheme.subtitle2,
),
Text(
'Followers',
style: Theme.of(context).textTheme.caption,
),
],
),
onTap: () {},
),
),
Expanded(
child: InkWell(
child: Column(
children: [
Text(
'600',
style: Theme.of(context).textTheme.subtitle2,
),
Text(
'Followings',
style: Theme.of(context).textTheme.caption,
),
],
),
onTap: () {},
),
),
],
),
),
Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text('Add Photos'),
),
),
SizedBox(
width: 10.0,
),
OutlinedButton(
onPressed: () {},
child: Icon(
IconBroken.Edit,
size: 20.0,
),
),
],
),
],
),
);
},
);
}
}
I am calling the getUserData in the main while app starts:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
Bloc.observer = MyBlocObserver();
DioHelper.init();
await CacheHelper.init();
Widget widget;
uId = CacheHelper.getData(key: 'uId');
if (uId != null) {
widget = SocialLayout();
} else {
widget = LoginScreen();
}
ErrorWidget.builder = (FlutterErrorDetails details) {
bool inDebug = false;
assert(() { inDebug = true; return true; }());
if (inDebug)
return ErrorWidget(details.exception);
return Container(
alignment: Alignment.center,
child: Text(
'Error! ${details.exception}',
style: TextStyle(color: Colors.yellow),
textDirection: TextDirection.ltr,
),
);
};
runApp(MyApp(
startWidget: widget,
));
}
class MyApp extends StatelessWidget {
final Widget startWidget;
MyApp({
this.startWidget,
});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (BuildContext context) => AppCubit()..getUserData()..handleScroll(),
child: BlocConsumer<AppCubit, AppStates>(
listener: (context, state) {},
builder: (context, state) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: lightTheme,ThemeMode.light,
themeMode: ThemeMode.light,
home: startWidget,
);
},
),
);
}
}
and this is the state class I have:
abstract class AppStates {}
class AppInitialState extends AppStates {}
class AppGetUserLoadingState extends AppStates {}
class AppGetUserSuccessState extends AppStates {}
class AppGetUserErrorState extends AppStates {
final String error;
AppGetUserErrorState(this.error);
}
class AppChangeBottomNavBarState extends AppStates {}
class AppChangeBottomSheetState extends AppStates {}
class AppChangeModeState extends AppStates {}
class AppHandleReverseHideBottomWidgetState extends AppStates {}
class AppHandleForwardHideBottomWidgetState extends AppStates {}
class AppHideBottomWidgetState extends AppStates {}
class AppShowBottomWidgetState extends AppStates {}
class AppNewPostState extends AppStates {}
I tried to create a condition in the listener as the below :
listener: (context, state) {
if(state is AppGetUserErrorState) {
return CircularProgressIndicator();
} else if (state is AppGetUserSuccessState){
return AppCubit.get(context).model;
}
},
After trying debugging I found the userModel is with null as the below image:
So After that I make the evaluation with the data is coming fromJson and I found every thing is normal as the below image:
but still not working..
How can I solve this?

