Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
  }
}
like image 431
Peter Birdsall Avatar asked Aug 19 '18 14:08

Peter Birdsall


1 Answers

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);
  }
like image 169
Sebastian Avatar answered Sep 24 '22 02:09

Sebastian