If I use a String as argument on a Provider the return of a Provider work But If I use a List
just like instagram my application has a feature that are the saved. One person can save post. For this I added a field in the users collection "save", this field is an array of "post id". This "post id" is used to fetch recipes (another collection) A user, if you want to see the posts you saved, click on a button and you are directed to this widget: ("getReceitaGuardadosProvider"-> this is the provider that I m talking about) receita=ref.read(getReceitaGuardadosProvider(user.guardados)); So if I give this provider a String as a argument It will return AsyncData(), but If I give a List as a argument It will return AsyncLoading()
import 'package:chefapp/features/auth/controleer/auth_controller.dart';
import 'package:chefapp/features/receita/receitas_controller/receitas_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:chefapp/core/common/loader.dart';
import 'package:chefapp/core/common/error.dart';
import '../../models/teste.dart';
import '../receita/receita_screen.dart';
class GuardadosPerfil extends ConsumerWidget {
const GuardadosPerfil({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final user = ref.watch(userProvider)!;
//final receitaa = //ref.read(getReceitaGuardadosProvider(user.guardados));
//print('receita');
//print(receitaa);
//print(user.guardados);
return Scaffold(
appBar: AppBar(
title: const Text('Receitas Guardadas'),
),
body: ref.read(getReceitaGuardadosProvider(user.guardados)).when(
data: (data) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
final receita = data[index];
return ReceitaScreen(
receita: receita, usermodel: user, aux: true);
},
);
},
error: (error, StackTrace) {
return ErrorText(error: error.toString());
},
loading: () => const Loader(),
),
);
}
}
Here is a controller Controller.dart
import 'package:chefapp/core/providers/storage_repository_provider.dart';
import 'package:chefapp/features/auth/controleer/auth_controller.dart';
import 'package:chefapp/models/receita_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:routemaster/routemaster.dart';
import 'package:uuid/uuid.dart';
import 'dart:io';
import '../../../core/utils.dart';
import '../../../models/coment_model.dart';
import '../../../models/teste.dart';
import '../../../models/usermodel.dart';
import '../receita_repository/receita_repository.dart';
final receitaControllerProvider =
StateNotifierProvider<ReceitaController, bool>((ref) {
final receitaRepository = ref.watch(receitaRepositoryProvider);
final storageRepository = ref.watch(storageRepositoryProvider);
return ReceitaController(
receitaRepository: receitaRepository,
ref: ref,
storageRepository: storageRepository);
});
final getReceitaGuardadosProvider = StreamProvider.family<List<Receita>, List<String>>(
(ref, guardados) {
print('ola');
final receitaController = ref.watch(receitaControllerProvider.notifier);
return receitaController.getReceitaGuardados(guardados);
},
);
class ReceitaController extends StateNotifier<bool> {
final ReceitaRepository _receitaRepository;
final Ref _ref;
final StorageRepository _storageRepository;
ReceitaController({
required ReceitaRepository receitaRepository,
required Ref ref,
required StorageRepository storageRepository,
}) : _receitaRepository = receitaRepository,
_ref = ref,
_storageRepository = storageRepository,
super(false);
Stream<List<Receita>> getReceitaGuardados(List<String> guardados) {
print('controller');
print(_receitaRepository.getReceitaGuardados(guardados));
return _receitaRepository.getReceitaGuardados(guardados);
}
}
Here is a repository.dart
import 'package:chefapp/core/constants/firebase_constants.dart';
import 'package:chefapp/core/failure.dart';
import 'package:chefapp/models/usermodel.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:fpdart/fpdart.dart';
import '../../../core/providers/firebase_providers.dart';
import '../../../core/type_defs.dart';
import '../../../models/coment_model.dart';
import '../../../models/receita_model.dart';
final receitaRepositoryProvider = Provider((ref) {
return ReceitaRepository(
firestore: ref.watch(firestoreProvider),
);
});
class ReceitaRepository {
final FirebaseFirestore _firestore;
ReceitaRepository({required FirebaseFirestore firestore})
: _firestore = firestore;
CollectionReference get _receitas =>
_firestore.collection(FirebaseConstants.receitasCollection);
CollectionReference get _users =>
_firestore.collection(FirebaseConstants.usersCollection);
CollectionReference get _comments =>
_firestore.collection(FirebaseConstants.commentsCollection);
Stream<List<Receita>> getReceitaGuardados(List<String> guardados) {
return _receitas
.where('id', whereIn: guardados)
.orderBy('datacriacao', descending: true)
.snapshots()
.map(
(event) => event.docs
.map(
(e) => Receita.fromMap(
e.data() as Map<String, dynamic>,
),
)
.toList(),
)
.map((list) {
print('repository');
print(list); // Adicione este print para imprimir a lista
return list;
});
}
}
All the print that I use seems to be alright. I really don't know what can I do. If I change the function to accept a String I can print the recipe but only one, If I use a List the loading: () => const Loader()
is the only thing I see.
some output of the prints Instance of '_MapStream<List, List>'
AsyncLoading<List>()
[Receita(id: 1197ddb0-fe44-11ed-ab9c-f3074e6ebbf2, name: rece, uidusuario: 9Fb0QHEtdjXUX4JsahQ7urcYI333, tempo: 32, calorias: 32, refeicoes: [Bebidas], categorias: [Peixe], ingredientes: 321, comofazer: 213123, likes: [], datacriacao: 2023-05-29 19:13:04.415, countcomentarios: 0, image: https://ift.tt/8KaCLw1]
Comments
Post a Comment