I am moving to a page using the Navigator.pushNamed
, but that after downloading a PDF ,
The first thing I do to show that it is downloading, I use a AlertDialog
that shows a CircularProgressIndicator
until the task is finished, then it will go to another screen.
- First, I make the call of the method that will show the
AlertDialog
to indicate that it is downloading.
When finished, it will go to the PDF screen .
print('descargando...');
archivo = await _descargando(ApiService.downPDF(npdf));
print('descarga finalizada');
if (archivo != null) {
await Navigator.pushNamed(context, 'viewPdf', arguments: archivo.path);
}
Method _descargando()
, in which I use Navigator.of(context).pop();
to close the AlertDialog
and proceed to continue with the navigation to another screen.
Future _descargando(Future future) async {
dynamic result;
await showDialog(
context: context,
builder: (context){
return AlertDialog(
title: Text('Descargando..'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FutureBuilder(
future: future,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
result = snapshot.data;
Navigator.of(context).pop();
}
return CircularProgressIndicator();
},
),
],
),
);
}
);
return result;
}
It changes the screen well for me and the download process is carried out without problem, but on the console it sends me the following exception.
════════ Exception caught by animation library ═════════════════════════════════
The following assertion was thrown while notifying status listeners for AnimationController:
setState() or markNeedsBuild() called during build.
This _ModalScope<dynamic> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: _ModalScope<dynamic>-[LabeledGlobalKey<_ModalScopeState<dynamic>>#78b06]
state: _ModalScopeState<dynamic>#4ff65
The widget which was currently being built when the offending call was made was: FutureBuilder<dynamic>
dirty
state: _FutureBuilderState<dynamic>#4506e
package:flutter/…/widgets/framework.dart:3687
#1 Element.markNeedsBuild
package:flutter/…/widgets/framework.dart:3702
#2 State.setState
package:flutter/…/widgets/framework.dart:1161
#3 _ModalScopeState._routeSetState
package:flutter/…/widgets/routes.dart:633
#4 ModalRoute.setState
package:flutter/…/widgets/routes.dart:732
...
The AnimationController notifying status listeners was: AnimationController#97aad(◀ 1.000; for _DialogRoute<dynamic>)
════════════════════════════════════════════════════════════════════════════════
It's because you're doing a rebuild when the widget is refreshing, you can put the pop in an async method:
And
builder
yoursFutureBuilder
looks like this: