从 firestore 和 firebase 存储中获取数据并在 flutter 中显示
Fetch data from firestore and firebase storage and displaying in flutter
我正在查询来自 firestore 的数据,并且使用这些数据,我正在从 firestore 获取图像数据。有了图像信息,我得到了 getdownloadUrl(); properties.docs.map 中的 print 语句不工作(我的意思是从 firestore 获取数据后没有任何工作),并且 bloc 中的加载指示器始终显示。
class FixedCubit extends Cubit<FixedState> {
FixedCubit() : super(FixedInitial());
void fetchProperties()async{
emit(FixedLoading());
List<UploadModel> upload = [];
final QuerySnapshot properties =
await FirebaseRepo.instance.getFixedHomes().get();
properties.docs.map((e) async {
print(e.get('uid'));
print(e.get('pickedFilesName')[0]);
final String url = await FirebaseRepo.instance
.downloadAllUserURLs(e.get('uid'), e.get('pickedFilesName')[0]);
upload.add(UploadModel.fromMap(e.data() as Map<String, dynamic>,url));
emit(FixedSuccess(upload));
});
}
}
这是我的状态class
part of 'fixed_cubit.dart';
@immutable
abstract class FixedState {}
class FixedInitial extends FixedState {}
class FixedLoading extends FixedState {}
class FixedSuccess extends FixedState {
final List<UploadModel> uploadModel;
FixedSuccess(this.uploadModel);
}
class FixedUnSuccess extends FixedState {
final String? msg;
FixedUnSuccess({this.msg});
}
这些是 firebase 方法
/// ========= Database Get ========== ///
Query getFixedHomes() {
return _firestore
.collectionGroup('properties')
.where('preference', isEqualTo: 'Fixed Price')
.orderBy('createdAt', descending: true);
}
Future<String> downloadAllUserURLs(String uid, String image) async {
return await firebase_storage.FirebaseStorage.instance
.ref('property/$uid')
.child(image)
.getDownloadURL()
.catchError((onError) {
print('On FirebaseStorage Exception $onError');
});
}
这是 Bloc
BlocConsumer<FixedCubit, FixedState>(
bloc: BlocProvider.of<FixedCubit>(context),
builder: (context, state) {
if (state is FixedLoading) {
return Center(child: getCircularProgress());
} else if (state is FixedSuccess) {
return SizedBox(
height: 300.0,
child: ListView.builder(
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: state.uploadModel.length,
itemBuilder: (BuildContext context, int index) {
final value = state.uploadModel[index];
print(
'File name ${state.uploadModel[index].pickedFilesName}');
print(state.uploadModel[index].pickedFilesName?[0]);
return GestureDetector(
onTap: () {
Navigator.of(context).pushNamed(
Routes.fixedDetails,
arguments: value.thumbnail);
},
child: CustomFixed(
image: value.thumbnail.toString(),
city: value.city.toString(),
startPrice: value.startPrice.toString(),
endPrice: value.endPrice.toString(),
title: value.title.toString()),
);
},
),
);
} else {
return const Text('Something went wrong');
}
},
listener: (context, state) {}),
更新
使用 for 循环代替 map() 函数
for (var e in properties.docs) {
final String url = await FirebaseRepo.instance
.downloadAllUserURLs(e.get('uid'), e.get('pickedFilesName')[0]);
upload.add(UploadModel.fromMap(e.data() as Map<String, dynamic>, url));
}
我正在查询来自 firestore 的数据,并且使用这些数据,我正在从 firestore 获取图像数据。有了图像信息,我得到了 getdownloadUrl(); properties.docs.map 中的 print 语句不工作(我的意思是从 firestore 获取数据后没有任何工作),并且 bloc 中的加载指示器始终显示。
class FixedCubit extends Cubit<FixedState> {
FixedCubit() : super(FixedInitial());
void fetchProperties()async{
emit(FixedLoading());
List<UploadModel> upload = [];
final QuerySnapshot properties =
await FirebaseRepo.instance.getFixedHomes().get();
properties.docs.map((e) async {
print(e.get('uid'));
print(e.get('pickedFilesName')[0]);
final String url = await FirebaseRepo.instance
.downloadAllUserURLs(e.get('uid'), e.get('pickedFilesName')[0]);
upload.add(UploadModel.fromMap(e.data() as Map<String, dynamic>,url));
emit(FixedSuccess(upload));
});
}
}
这是我的状态class
part of 'fixed_cubit.dart';
@immutable
abstract class FixedState {}
class FixedInitial extends FixedState {}
class FixedLoading extends FixedState {}
class FixedSuccess extends FixedState {
final List<UploadModel> uploadModel;
FixedSuccess(this.uploadModel);
}
class FixedUnSuccess extends FixedState {
final String? msg;
FixedUnSuccess({this.msg});
}
这些是 firebase 方法
/// ========= Database Get ========== ///
Query getFixedHomes() {
return _firestore
.collectionGroup('properties')
.where('preference', isEqualTo: 'Fixed Price')
.orderBy('createdAt', descending: true);
}
Future<String> downloadAllUserURLs(String uid, String image) async {
return await firebase_storage.FirebaseStorage.instance
.ref('property/$uid')
.child(image)
.getDownloadURL()
.catchError((onError) {
print('On FirebaseStorage Exception $onError');
});
}
这是 Bloc
BlocConsumer<FixedCubit, FixedState>(
bloc: BlocProvider.of<FixedCubit>(context),
builder: (context, state) {
if (state is FixedLoading) {
return Center(child: getCircularProgress());
} else if (state is FixedSuccess) {
return SizedBox(
height: 300.0,
child: ListView.builder(
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: state.uploadModel.length,
itemBuilder: (BuildContext context, int index) {
final value = state.uploadModel[index];
print(
'File name ${state.uploadModel[index].pickedFilesName}');
print(state.uploadModel[index].pickedFilesName?[0]);
return GestureDetector(
onTap: () {
Navigator.of(context).pushNamed(
Routes.fixedDetails,
arguments: value.thumbnail);
},
child: CustomFixed(
image: value.thumbnail.toString(),
city: value.city.toString(),
startPrice: value.startPrice.toString(),
endPrice: value.endPrice.toString(),
title: value.title.toString()),
);
},
),
);
} else {
return const Text('Something went wrong');
}
},
listener: (context, state) {}),
更新 使用 for 循环代替 map() 函数
for (var e in properties.docs) {
final String url = await FirebaseRepo.instance
.downloadAllUserURLs(e.get('uid'), e.get('pickedFilesName')[0]);
upload.add(UploadModel.fromMap(e.data() as Map<String, dynamic>, url));
}