Peter Birdsall
Peter Birdsall

Reputation: 3425

In Flutter what value do you use to initialize WillPopCallback variable?

I've created a custom widget (below) for WillPopScope, how do you initialize WillPopCallback onWillPop?

I get warnings that the onWillPop variable is not initialized and haven't found how is should be initialized.

In the following implementation of PlatformWillPopScope the WillPopCallback onWillPop is just passed thru to the Flutter defined widget WillPopScope. From Dart Analysis window I get the following error:

warning: The final variable 'onWillPop' must be initialized. (final_not_initialized_constructor_1 at [draw_navigation_app] lib/common/platform/platformWillPopScope.dart:15)

I'm just looking to initialize onWillPop to any value, as the actual value will be supplied from the constructor.

class PlatformWillPopScope extends StatelessWidget{

final Key key;
final Widget child;
final WillPopCallback onWillPop;

final EdgeInsetsGeometry paddingExternal = EdgeInsets.all(0.0);

PlatformWillPopScope({
this.key,
this.child,
onWillPop
})  : assert(child != null),
    super(key: key);

@override
Widget build(BuildContext context) {

return Platform.isIOS ?
    Padding(padding: paddingExternal == null ? EdgeInsets.all(0.0) : 
paddingExternal,
      child: child,
      )
:
    WillPopScope(key: key,
      child: child,
      onWillPop: onWillPop);
  }
}

Upvotes: 5

Views: 3390

Answers (2)

Peter Birdsall
Peter Birdsall

Reputation: 3425

Thanks to Sebastian, it lead me to the answer.

final WillPopCallback onWillPop = () {return Future.value(false);};

Upvotes: 2

Sebastian
Sebastian

Reputation: 3894

I agree that there is missing information and context, I use willPopScope like this, maybe it helps

Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        appBar: _buildAppbar(),
        body: Container(),
        bottomNavigationBar: _buildBottomNavBar(),
        floatingActionButton: _buildFAB()
      ),
    );
  }

And this is the function

Future<bool> _onWillPop() {
    if(_currentIndex == 0) {
      return Future.value(true);
    }
    _navigate(0);
    return Future.value(false);
  }

Upvotes: 3

Related Questions