When the onConfirmBtnTap
button is pressed in the alert, control is transferred to the previous screen, where this value is displayed in the navigation processing function. However, null comes.
Why doesn’t the value come from alert? (it’s in onTapFunction
)
// start
class FirstPage extends StatefulWidget {...}
class _FirstPageState extends State<FirstPage> {
@override
Widget build(BuildContext context) {
debugPrint('-------------building outer----------'); //true
return Scaffold(
body: Column(
children: [
...
InnerWidget(key: UniqueKey()), //unique
],
),
);
}
}
// Inner widget
class InnerWidget extends StatefulWidget {...}
class _InnerWidgetState extends State<InnerWidget> {
...
@override
Widget build(BuildContext context) {
debugPrint('building inner widget...');
...
// in gesture detector
onTap: () {
var productData = ...;
onTapFunction(context, Group(productData));
},
}
// onTapFunction is defined in InnerWidget
onTapFunction(BuildContext context, Widget route) async {
final reLoadPage = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => route,
),
);
if (reLoadPage ?? false) { // the value of reloadPage is null
debugPrint('---------------reLoadPage: $reLoadPage');
setState(() {});
}
}
} // Inner widget end
// on Group page a button opens alert dialog
onPressed: () {
storage.removeProduct(productData);
if (!context.mounted) return;
productDeletedSuccessDialog(context); //opens alert dialog
},
// in alert dialog on confirm it pops back and pass value
onConfirmBtnTap: () {
debugPrint('product $productData removed');
Navigator.pop(context, true); // it should pass `true` into InnerWidget (?)
});
p.s. I tried to explain and show the code as strictly and clearly as possible in order to understand the essence of what is happening.
2
Answers
I have updated the onTapFunction. Once to pop from the second screen, then clause will trigger.
Try like this