I'm new to this world of providers and changenotifiers in flutter, so I don't know how to afford this issue.
I have a widget (NuevoAudiolibro) that represents a new audiobook, and I have a list of chapters associated to this audiobook that are represented in a list of cards. This list of cards is the widget ListaCapitulosCompactos. The problem is, when you enter to the screen of NuevoAudiolibro, I have to get from flutter firebase the list of chapters to fill the widget ListaCapitulosCompactos. It was easy in the beggining because I didn't use a changenotifier, but, as in this screen you can edit these chapters and I want these upates to be reflected in the list, I needed a Changenotifier. I made some trials initializing the list of chapters directly in the changenotifier. So the changenotifier was like this:
class CapitulosAudiolibro with ChangeNotifier {
final List<Capitulo> _listaCapitulosAudiolibro = [
Capitulo(id: '1', idAudiolibro: 'sbE2gZpfTagd9EXOFibr', titulo: 'Capítulo 1 - Reino de sombras', duracion: '17:35', esBorrador: true, mostrarExpandido: true, capituloNuevo: false, descripcion: 'En un mundo en llamas, la princesa Sofía decide adentrarse en lo más profundo del bosque, donde sólo habita la oscuridad', ),
Capitulo(id: '2', idAudiolibro: 'sbE2gZpfTagd9EXOFibr', titulo: 'Capítulo 2 - Acechando en la noche', duracion: '12:34', esBorrador: true, mostrarExpandido: false, capituloNuevo: false,)
];
//update(List<Capitulo> _capsAudiolibro) => _listaCapitulosAudiolibro = _capsAudiolibro;
List<Capitulo> get listaCapitulosAudiolibro => _listaCapitulosAudiolibro;
//To add a new chapter to the list
void anyadirCapitulo(Capitulo nuevoCap){
_listaCapitulosAudiolibro.add(nuevoCap);
notifyListeners();
}
//To update one chapter in the list
void modificarCapitulo(Capitulo capAModificar){
for(Capitulo cap in _listaCapitulosAudiolibro){
if(cap.id == capAModificar.id){
var indiceCap = _listaCapitulosAudiolibro.indexOf(cap);
_listaCapitulosAudiolibro[indiceCap] = capAModificar;
}
}
notifyListeners();
}
}
And I had a consumer in the list, so, everytime a chapter in the list was changed, the UI changed too:
Consumer<CapitulosAudiolibro>(
builder: (context, capitulosAudiolibro, child){
return ListaCapitulosCompactos(listaCapitulos: capitulosAudiolibro.listaCapitulosAudiolibro, ); //providerEditarCapitulos: capitulosAudiolibro,
}),
But I need to get the initial data for the list from cloud firebase, so I need to update the list in the changenotifier when my async method charges this information. However, I don't know how to do this. I've tried with FutureProvider, and FutureBuilder, even creating a method in the ChangeNotifier that fills the list from the db when the charge is completed and calling it from the initState() method of NuevoAudiolibro widget. None of this has worked, probably because I don't know the properly way to do it. To get data from db and initialize it in the CapitulosAudiolibro ChangeNotifier, I've tried things like this, creating a method that fills the list directly in the ChangeNotifier class:
Future<void> rellenarCapsIniciales(String idAudiolibro) async{
_listaCapitulosAudiolibro = await CapituloService().getCapitulosByIdAudiolibro(idAudiolibro); //method that gets chapters data from db
notifyListeners();
}
And then creating directly one provider of this ChangeNotifier in the NuevoAudiolibro widget, and calling to this method in its initState method:
class _NuevoAudiolibroState extends State<NuevoAudiolibro> {
var futureBuilderListaCaps;
@override
void initState(){
super.initState();
//recuperarCapitulosAsociados();
futureBuilderListaCaps = Provider.of<CapitulosAudiolibro>(context).rellenarCapsIniciales(widget.audiolibro.id);
}
But I have errors like this all the time, and I don't know why. When I see the examples in google, people always use Provider.of(context, listen: false)... and it works for them:
I created a parent widget for this NuevoAudiolibro widget, so it could received the ChangeNotifier as a parameter from its father, but it didn't work neither.
I don't know if I explained well, but simply I need to initialize data from db in the ChangeNotifier, the rest was working fine with the consumers.