skip to Main Content

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


  1. I have updated the onTapFunction. Once to pop from the second screen, then clause will trigger.

    // onTapFunction used and defined in InnerWidget
    onTapFunction(BuildContext context, Widget route) async {
        final reLoadPage = await Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => route,
          ),
        ).then((value){
          if (reLoadPage ?? false) {
              debugPrint('---------------reLoadPage: $reLoadPage');
              setState(() {});
          }
      });
    } 
    
    Login or Signup to reply.
  2. Try like this

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      MyHomePageState createState() => MyHomePageState();
    }
    
    class MyHomePageState extends State<MyHomePage> {
     
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Demo'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [InnerWidget()],
            ),
          ),
        );
      }
    }
    
    class SecondPage extends StatefulWidget {
      SecondPage({Key? key}) : super(key: key);
    
      @override
      State<SecondPage> createState() => _SecondPageState();
    }
    
    class _SecondPageState extends State<SecondPage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                Navigator.pop(context, true);
              },
              child: const Text('Go Back'),
            ),
          ),
        );
      }
    }
    
    class InnerWidget extends StatefulWidget {
      InnerWidget({Key? key}) : super(key: key);
    
      @override
      State<InnerWidget> createState() => _InnerWidgetState();
    }
    
    class _InnerWidgetState extends State<InnerWidget> {
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            ElevatedButton(
              onPressed: () async {
                final reLoadPage = await Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => SecondPage(),
                  ),
                );
                if (reLoadPage ?? false) {
                  debugPrint('---------------reLoadPage: $reLoadPage');
                  setState(() {});
                }
              },
              child: const Text('Open Page'),
            ),
          ],
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search