Reputation: 877
I am trying to define a parameter as callback in a StatefulWidget
widget and using that widget as const child widget in another StatefulWidget
widget. But it is not allowing me to pass any argument with const child widget.
Here is my code snippet:
Container(
child: const LeftSidePanel(
checking: (value)
{
},
),
),
Here is the class of child widget used in above code:
class LeftSidePanel extends StatefulWidget {
final Function(bool?) checking;
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return LeftPanelWidgetState();
}
const LeftSidePanel({
required this.checking,
});
}
In the first code snippet, it is giving error saying "A value of type 'Null' can't be assigned to a parameter of type 'dynamic Function(bool?)' in a const constructor. (Documentation) Try using a subtype, or removing the keyword 'const'."
Can anyone help me with this issue. How can I solve this. Or if you can suggest me some other way of accessing const child widget data or passing data from const child widget to parent widget.
Thanks in advance
Upvotes: 3
Views: 3300
Reputation: 111
As far as I know only top level functions or static functions are considered const values in dart. Thus the lambda you are passing in for checking
is not a const value. Therefore you cannot use the const version of the constructor.
To solve this you could either not use the const version of the constructor by omitting the const keyword or pass a top level function to checking
.
Upvotes: 0
Reputation: 2910
You are passing a callback that is not const
(Invalid constant value) and therefore, you should just remove const
that will work for you
Container(
child: LeftSidePanel(
checking: (value) {},
),
);
Upvotes: 0
Reputation: 21
Removing const will probably solve your issue. I tried running your code and removing it solved it. To answer why, I guess const doesn't allow callbacks or methods.
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: LeftSidePanel(
checking: (value) {},
),
),
);
}
}
class LeftSidePanel extends StatefulWidget {
final Function(bool?) checking;
const LeftSidePanel({Key? key, required this.checking}) : super(key: key);
@override
State<LeftSidePanel> createState() => _LeftSidePanelState();
}
class _LeftSidePanelState extends State<LeftSidePanel> {
@override
Widget build(BuildContext context) {
return Container();
}
}
Upvotes: 0
Reputation: 4523
Remove the const parameter before LeftSidePanel:
import 'package:flutter/material.dart';
class LeftSidePanel extends StatefulWidget {
final Function(bool?) checking;
const LeftSidePanel({Key? key, required this.checking}) : super(key: key);
@override
State<LeftSidePanel> createState() => _LeftSidePanelState();
}
class _LeftSidePanelState extends State<LeftSidePanel> {
@override
Widget build(BuildContext context) {
return Container();
}
}
class Test extends StatelessWidget {
const Test({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: LeftSidePanel(
checking: (value) {},
),
);
}
}
That's because the code of your Function is not an const, so LeftSidePanel can't be a const.
Upvotes: 2