参数类型 'Function' 无法分配给参数类型 'void Function(bool)?'
The argument type 'Function' can't be assigned to the parameter type 'void Function(bool)?'
enter image description here
import 'package:flutter/material.dart';
import '../widgets/main_drawer.dart';
class FiltersScreen extends StatefulWidget { static const routeName
= '/filtters';
@override State<FiltersScreen> createState() =>
_FiltersScreenState(); }
class _FiltersScreenState extends State<FiltersScreen> { var
_glutenFree = false; var _vegan = false; var _vegetarian = false; var _lactoseFree = false;
Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
Function updateValue, ) {
return SwitchListTile(
title: Text(title),
subtitle: Text(description),
value: currentValue,
onChanged: updateValue,
); }
@override Widget build(BuildContext context) {
return Scaffold(
drawer: MainDrawer(),
appBar: AppBar(
title: Text('Filtters'),
),
body: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(20),
child: Text(
'Adjust Your Meal Selection!',
style: Theme.of(context).textTheme.subtitle1,
),
),
Expanded(
child: ListView(
children: <Widget>[
_buildSwitchListTile(
'Gluten-Free',
'Only Include Gluten-Free Meals.',
_glutenFree,
setState(
(newValue) {
_glutenFree = newValue;
},
),
),
],
),
)
],
),
); } }
我尝试将 Function updateValue 更改为最终 Function(bool newValue) updateValue
但是我在 setState 中遇到了一个问题 =>> 问题是 (
This expression has a type of 'void' so its value can't be used. Try
checking to see if you're using the correct API; there might be a
function or call that returns void you didn't expect. Also check type
parameters and variables which might also be void
)
将参数 updateValue
的类型从 Function
更改为 void Function(bool)?
:
Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
void Function(bool)? updateValue,
) {
return SwitchListTile(
title: Text(title),
subtitle: Text(description),
value: currentValue,
onChanged: updateValue,
);
}
修复方法如下,首先按照你说的改
Function onChanged,
对此:
void Function(bool) onChanged,
这意味着onChanged
会有一个参数,而return什么都没有,所以当你传递它时,你需要使用一个lambda函数:
_buildSwitchListTile(
'Gluten-Free',
'Only Include Gluten-Free Meals.',
_glutenFree,
(newValue) => setState(
() {
_glutenFree = newValue;
},
),
),
enter image description here
import 'package:flutter/material.dart';
import '../widgets/main_drawer.dart';
class FiltersScreen extends StatefulWidget { static const routeName
= '/filtters';
@override State<FiltersScreen> createState() =>
_FiltersScreenState(); }
class _FiltersScreenState extends State<FiltersScreen> { var
_glutenFree = false; var _vegan = false; var _vegetarian = false; var _lactoseFree = false;
Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
Function updateValue, ) {
return SwitchListTile(
title: Text(title),
subtitle: Text(description),
value: currentValue,
onChanged: updateValue,
); }
@override Widget build(BuildContext context) {
return Scaffold(
drawer: MainDrawer(),
appBar: AppBar(
title: Text('Filtters'),
),
body: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(20),
child: Text(
'Adjust Your Meal Selection!',
style: Theme.of(context).textTheme.subtitle1,
),
),
Expanded(
child: ListView(
children: <Widget>[
_buildSwitchListTile(
'Gluten-Free',
'Only Include Gluten-Free Meals.',
_glutenFree,
setState(
(newValue) {
_glutenFree = newValue;
},
),
),
],
),
)
],
),
); } }
我尝试将 Function updateValue 更改为最终 Function(bool newValue) updateValue 但是我在 setState 中遇到了一个问题 =>> 问题是 (
This expression has a type of 'void' so its value can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void
)
将参数 updateValue
的类型从 Function
更改为 void Function(bool)?
:
Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
void Function(bool)? updateValue,
) {
return SwitchListTile(
title: Text(title),
subtitle: Text(description),
value: currentValue,
onChanged: updateValue,
);
}
修复方法如下,首先按照你说的改
Function onChanged,
对此:
void Function(bool) onChanged,
这意味着onChanged
会有一个参数,而return什么都没有,所以当你传递它时,你需要使用一个lambda函数:
_buildSwitchListTile(
'Gluten-Free',
'Only Include Gluten-Free Meals.',
_glutenFree,
(newValue) => setState(
() {
_glutenFree = newValue;
},
),
),