'Using setState in void function and get a warning that the void function(dynamic) cannot be assigned to the parameter type 'void Function()'
I'm getting a warning that The argument type 'void Function(dynamic)' can't be assigned to the parameter type 'void Function()' which is line right below the String dialogText. I used this function to pass data from Child widget, TopAppBar, to Parent widget which is MyApp. From the child widget, TopAppBar, I used TextFormField to type the word to change the text when it's submitted.
I saw the other codes that have no problem, so I don't know how to solve this problem.
Please help me! Thank you!
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int count = 0;
var nameList = ['one','two','three','four'];
String dialogText = "Hello!";
void _changeDialogText(value){
setState((value){
dialogText = value;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: TopAppBar(state: _changeDialogText,),
body: Column(
children: [
Container(
height: 400,
child: (
ListView(
children: List.generate(
nameList.length, (index) => CustomListTile(title: nameList[index])),
)
)
),
Container(
height: 20, child: Text(count.toString())
),
Container(
height: 20, child: Text(dialogText)
),
],
),
floatingActionButton: FloatingActionButton(
child: Text('Button'),
onPressed: (){
print('a');
setState(
(){
count++;
}
);
},
),
bottomNavigationBar: BottomBar(),
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked
);
}
}
class TopAppBar extends StatefulWidget with PreferredSizeWidget{
const TopAppBar({Key? key, required this.state(value)}) : super(key: key);
final Function(String value) state;
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
State<TopAppBar> createState() => _TopAppBarState();
}
class _TopAppBarState extends State<TopAppBar> {
final TopAppBarData _topAppBarData = TopAppBarData();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Form(
child:
AppBar(
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: <Color>[Color.fromRGBO(180, 44, 77, 1) , Color.fromRGBO(242, 114, 59, 1)]
)
),
),
elevation: 0.0,
centerTitle: false,
title: Row(
children: [
Text(
'SSG',
style: TextStyle(
color: Colors.white,
),
),
Icon(
Icons.expand_more,
color: Colors.black,
)
],
),
actions: [
Padding(
padding: EdgeInsets.only(right: 30.0),
child: IconButton(
icon: Icon(Icons.search),
color: Colors.black,
onPressed: (){
showDialog(context: context, builder: (context){
return AlertDialog(
title: Text('Search'),
content: TextFormField(
onChanged: (value) {
widget.state(value);
},
decoration: InputDecoration(hintText: "Text Field in Dialog"),
),
actions: <Widget>[
ElevatedButton(
child: Text('CANCEL'),
onPressed: () {
setState(() {
Navigator.pop(context);
});
},
),
ElevatedButton(
child: Text('OK'),
onPressed: () {
setState(() {
Navigator.pop(context);
});
},
),
],
);
});
},
)
),
Padding(
padding: EdgeInsets.only(right: 30.0),
child: GestureDetector(
onTap: () {},
child: Icon(
Icons.menu,
color: Colors.black,
),
)
),
Padding(
padding: EdgeInsets.only(right: 30.0),
child: GestureDetector(
onTap: () {},
child: Icon(
Icons.notification_add,
color: Colors.black,
),
)
),
],
),
);
}
}
Solution 1:[1]
the setState() function doesn't pass any arguments to the callback. The code should look like this:
setState(() {
dialogText = value;
});
Solution 2:[2]
If you omit types, dynamic is assumed by the compiler. Don't do that. Turn on your linter (it should be on by default) and it will warn you about it.
final /* --> */ void /* <-- */ Function(String value) state;
and
void _changeDialogText( /* --> */ String /* <-- */ value){
Now, the signatures match.
Solution 3:[3]
change this
void _changeDialogText(value){
setState((value){
dialogText = value;
});
}
to
void _changeDialogText(String value){
setState((value){
dialogText = value;
});
}
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 | Libertas |
| Solution 2 | nvoigt |
| Solution 3 | manhtuan21 |
