'Error: The parameter 'scaffoldKey' can't have a value of 'null' [...] 'GlobalKey<ScaffoldState>'
When trying to run the function below, you get this error:
home page
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/appdata.dart';
import '../partials/customappbar.dart';
class HomePage extends StatelessWidget {
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
TextStyle styles = TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
fontFamily: 'Helvetica Neue'
);
@override
Widget build(BuildContext context){
return Consumer<AppData>(
builder: (ctx, appdata, child) => Scaffold(
key: _scaffoldKey,
appBar: CustomAppBar(
scaffoldKey: _scaffoldKey,
title: 'Pagina Home',
),
drawer: Drawer(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.only(bottom: 30),
child: Text("Bem vindo(a) ao:", style: styles,)
),
Image.asset('lib/assets/devstravel_logo.png', width: 200,),
Container(
margin: EdgeInsets.only(top: 30),
child:
Text("Seu guia de viagem perfeito", style: styles,
)
)
],
)
),
)
);
}
}
here customappbar
import 'package:flutter/material.dart';
PreferredSizeWidget CustomAppBar ({
GlobalKey<ScaffoldState> scaffoldKey,
String title = '',
bool hideSearch = false,
bool showBack = false
})
{
IconButton drawerIcon = IconButton (
icon: Icon(Icons.menu, color: Colors.black, size: 30),
onPressed: (){}
);
IconButton backIcon = IconButton (
icon: Icon(Icons.arrow_back, color: Colors.black, size: 30),
onPressed: (){
scaffoldKey.currentState.openDrawer();
}
);
IconButton leadingButton = drawerIcon;
if (showBack){
leadingButton = backIcon;
}
return AppBar(
backgroundColor: Colors.white,
elevation: 0,
centerTitle: false, //aqui eu deixo o texto centralizado
title: Text(
title,
style: TextStyle(
color:Colors.black,
fontSize: 20,
fontWeight: FontWeight.bold,
fontFamily: 'Helvetica Neue'
),
),
leading: leadingButton,
actions: <Widget>[
!hideSearch ? IconButton(
icon: Icon(Icons.search, color: Colors.black, size: 30),
onPressed: (){}
) : Container()
],
);
}
error message:
*: Error: The parameter 'scaffoldKey' can't have a value of 'null' because of its type 'GlobalKey', but the implicit default value is 'null'. lib/…/partials/customappbar.dart:5
- 'GlobalKey' is from 'package:flutter/src/widgets/framework.dart' ('/C:/flutter/packages/flutter/lib/src/widgets/framework.dart'). package:flutter/…/widgets/framework.dart:1
- 'ScaffoldState' is from 'package:flutter/src/material/scaffold.dart' ('/C:/flutter/packages/flutter/lib/src/material/scaffold.dart'). package:flutter/…/material/scaffold.dart:1 Try adding either an explicit non-'null' default value or the 'required' modifier. GlobalKey scaffoldKey,*
What is the problem?? looks like everything is ok.
Solution 1:[1]
You are using named constructor which is contains optional parameters. You need to add required or provide default value on parameters.
PreferredSizeWidget CustomAppBar({
required GlobalKey<ScaffoldState> scaffoldKey, // add required here
String title = '',
bool hideSearch = false,
bool showBack = false,
}) {
And to open drawer using drawerIcon, do
IconButton drawerIcon = IconButton(
icon: Icon(Icons.menu, color: Colors.black, size: 30),
onPressed: () {
scaffoldKey.currentState?.openDrawer();
});
More about required-named-parameters.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
