【发布时间】:2020-08-14 01:44:18
【问题描述】:
我正在尝试使用连续的多个开关和文本来自动化页面。我制作了一个自定义小部件,以便在需要同时渲染字符串和开关时可以调用它。但我得到了一个错误。
错误是:
════════ Exception caught by gesture ═══════════════════════════════════════════════════
The method 'call' was called on null.
Receiver: null
Tried calling: call(false)
══════════════════════════════════════════════════════════════════════════════
这是我的代码:
class FavoriteScreen extends StatefulWidget {
@override
_FavoriteScreenState createState() => _FavoriteScreenState();
}
class _FavoriteScreenState extends State<FavoriteScreen> {
Widget stringSwitch(
String text, bool val, bool newval, Function onChangedMethod) {
return Padding(
padding: EdgeInsets.only(top: 22.0, left: 16.0, right: 16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
text,
style: TextStyle(
fontSize: 12.0,
fontFamily: 'Roboto',
fontWeight: FontWeight.w600,
color: Hexcolor('#676767')),
),
Spacer(),
CupertinoSwitch(
trackColor: Hexcolor('#dee7f5'),
activeColor: Hexcolor('#0565ac'),
value: val,
onChanged: (newval) {
onChangedMethod(newval);
})
],
),
);
}
bool val1 = true, val2 = false, val3 = true;
bool newval1, newval2, newval3;
onChangedFunction1(bool newval1) {
setState(() {
val1 = newval1;
});
}
onChangedFunction2(bool newval2) {
setState(() {
val2 = newval2;
});
}
onChangedFunction3(bool newval3) {
setState(() {
val3 = newval3;
});
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: Hexcolor('#e9f1fe'),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
stringSwitch('ABC', val1, newval1, onChangedFunction1(newval1)),
stringSwitch('PQR', val2, newval2, onChangedFunction2(newval2)),
stringSwitch('XYZ', val3, newval3, onChangedFunction3(newval3)),
],
),
),
);
}
}
你能帮我解决这个问题吗?更正的代码 sn-p 会很棒。
【问题讨论】:
-
方法 'call' 在 null 上被调用。但是您没有包含调用“调用”函数的代码。
-
@Kent 雅,我只是想通了。谢谢!
标签: flutter dart flutter-layout