从Firebase检索数据时出错(在null上调用了getter';cover';)
我试图从从Firebase检索数据时出错(在null上调用了getter';cover';),firebase,flutter,dart,Firebase,Flutter,Dart,我试图从Firebase调用数据,但在第一次启动登录时发现以下错误,这是以下错误: The following NoSuchMethodError was thrown building BlocBuilder<AppCubit, AppStates>(dirty, state: _BlocBuilderBaseState<AppCubit, AppStates>#b335d): The getter 'cover' was called on null. Receive
Firebase
调用数据,但在第一次启动登录时发现以下错误,这是以下错误:
The following NoSuchMethodError was thrown building BlocBuilder<AppCubit, AppStates>(dirty, state: _BlocBuilderBaseState<AppCubit, AppStates>#b335d):
The getter 'cover' was called on null.
Receiver: null
Tried calling: cover
这是我的全部代码:
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mysocially/shared/cubit/cubit.dart';
import 'package:mysocially/shared/cubit/states.dart';
import 'package:mysocially/styles/icon_broken.dart';
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;
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: 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,
),
),
],
),
],
),
);
},
);
}
}
导入“包装:颤振/材料.省道”;
进口“包装:颤振团/颤振团.飞镖”;
导入“包:mysocially/shared/cubit/cubit.dart”;
导入“package:mysocially/shared/cubit/states.dart”;
导入“package:mysocially/styles/icon_break.dart”;
类设置屏幕扩展无状态小部件{
常量设置屏幕({Key}):超级(Key:Key);
@凌驾
小部件构建(构建上下文){
返回BlocConsumer(
侦听器:(上下文,状态){},
生成器:(上下文、状态){
var userModel=AppCubit.get(context.model);
返回填充(
填充:常数边集全部(8.0),
子:列(
儿童:[
容器(
身高:190.0,
子:堆栈(
对齐:对齐方向.bottomCenter,
儿童:[
对齐(
对齐:对齐方向.topCenter,
子:容器(
宽度:double.infinity,
身高:140.0,
装饰:盒子装饰(
borderRadius:仅限borderRadius(
左上:半径。圆形(4.0),
右上角:半径。圆形(4.0),
),
图像:装饰图像(
图片:NetworkImage(
“${userModel.cover}”,
),
适合:BoxFit.cover,
),
),
),
),
圆形(
半径:51.0,
背景颜色:
主题。背景。脚手架背景颜色,
孩子:圆环星(
//半径:1.5米高*0.025米,
半径:50.0,
背景图片:NetworkImage(
“${userModel.image}”,
),
),
),
],
),
),
大小盒子(
身高:5.0,
),
正文(
“${userModel.name}”,
样式:Theme.of(context).textTheme.bodyText1,
),
正文(
“${userModel.bio}”,
样式:Theme.of(context).textTheme.caption,
),
填充物(
填充:常量边集。对称(垂直:20.0),
孩子:排(
儿童:[
扩大(
孩子:InkWell(
子:列(
儿童:[
正文(
'100',
风格:Theme.of(context).textTheme.subtitle2,
),
正文(
"职位",,
样式:Theme.of(context).textTheme.caption,
),
],
),
onTap:(){},
),
),
扩大(
孩子:InkWell(
子:列(
儿童:[
正文(
'100',
风格:Theme.of(context).textTheme.subtitle2,
),
正文(
"职位",,
样式:Theme.of(context).textTheme.caption,
),
],
),
onTap:(){},
),
),
扩大(
孩子:InkWell(
子:列(
儿童:[
正文(
'62',
风格:Theme.of(context).textTheme.subtitle2,
),
正文(
“照片”,
样式:Theme.of(context).textTheme.caption,
),
],
),
onTap:(){},
),
),
扩大(
孩子:InkWell(
子:列(
儿童:[
正文(
'200',
风格:Theme.of(context).textTheme.subtitle2,
),
正文(
“追随者”,
样式:Theme.of(context).textTheme.caption,
),
],
),
onTap:(){},
),
),
扩大(
孩子:InkWell(
子:列(
儿童:[
正文(
'600',
风格:Theme.of(context).textTheme.subtitle2,
),
正文(
“以下内容”,
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mysocially/shared/cubit/cubit.dart';
import 'package:mysocially/shared/cubit/states.dart';
import 'package:mysocially/styles/icon_broken.dart';
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;
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: 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,
),
),
],
),
],
),
);
},
);
}
}
class AppCubit extends Cubit<AppStates> {
AppCubit() : super(AppInitialState());
static AppCubit get(context) => BlocProvider.of(context);
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(),
),
);
});
}
}