flutter - “Null”类型不是“List”类型的子类型'函数结果'
问题描述
该代码应该可以在旧版本中正常工作,因为它是现成的解决方案,但我遇到了错误:
“Null”类型不是“函数结果”的“列表”类型的子类型
这是我的代码:
import 'package:first_flutter_project/shared/menu_bottom.dart';
import 'package:first_flutter_project/shared/menu_drawer.dart';
import 'package:flutter/material.dart';
class BmiScreen extends StatefulWidget {
const BmiScreen({Key? key}) : super(key: key);
@override
_BmiScreenState createState() => _BmiScreenState();
}
class _BmiScreenState extends State<BmiScreen> {
final double fontSize = 18;
final TextEditingController txtHeight = TextEditingController();
final TextEditingController txtWeight = TextEditingController();
String result = '';
bool isMetric = true;
bool isImperial = false;
double? height;
double? weight;
String heightMessage = '';
String weightMessage = '';
late List<bool> isSelected;
@override
void initState() {
isSelected = [isMetric, isImperial];
super.initState();
}
@override
Widget build(BuildContext context) {
heightMessage =
'Please insert your height in ' + ((isMetric) ? 'meters' : 'inches');
weightMessage =
'Please insert your weight in ' + ((isMetric) ? 'kilos' : 'pounds');
return Scaffold(
appBar: AppBar(
title: Text('BMI Calculator'),
),
drawer: MenuDrawer(),
bottomNavigationBar: MenuBottom(),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ToggleButtons(
children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Text(
'Metric',
style: TextStyle(
fontSize: fontSize,
),
)),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Text(
'Imperial',
style: TextStyle(fontSize: fontSize),
)),
],
isSelected: isSelected,
onPressed: toggleMeasure,
),
TextField(
controller: txtHeight,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: heightMessage,
),
),
TextField(
controller: txtWeight,
keyboardType: TextInputType.number,
decoration: InputDecoration(hintText: weightMessage),
),
ElevatedButton(
onPressed: findBMI,
child: Text('Calculate BMI',
style: TextStyle(
fontSize: fontSize,
))),
Text(result,
style: TextStyle(
fontSize: fontSize,
))
],
),
),
);
}
void toggleMeasure(value) {
if (value == 0) {
isMetric = true;
isImperial = false;
} else {
isMetric = false;
isImperial = true;
}
setState(() {
isSelected = [isMetric, isImperial];
});
}
void findBMI() {
double bmi = 0;
double height = double.tryParse(txtHeight.text) ?? 0;
double weight = double.tryParse(txtWeight.text) ?? 0;
if (isMetric) {
bmi = weight / (height * height);
} else {
bmi = (weight * 703) / (height * height);
}
setState(() {
result = 'Your BMI is ' + bmi.toStringAsFixed(2);
});
}
}
这是堆栈跟踪:
The relevant error-causing widget was:
BmiScreen BmiScreen:file:///C:/Users/San4o/AndroidStudioProjects/first_flutter_project/lib/main.dart:18:30
When the exception was thrown, this was the stack:
#0 _BmiScreenState.isSelected (package:first_flutter_project/screens/bmi_screen.dart)
#1 _BmiScreenState.build (package:first_flutter_project/screens/bmi_screen.dart:67:27)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4782:27)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4665:15)
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
#5 Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
#6 StatefulElement.update (package:flutter/src/widgets/framework.dart:4872:5)
#7 Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
我认为初始化字段可能存在一些问题:
late List<bool> isSelected;
我试图用可为空来改变它,但后来我遇到了错误:
lib/screens/bmi_screen.dart:67:27: Error: The argument type 'List<bool>?' can't be assigned to the parameter type 'List<bool>' because 'List<bool>?' is nullable and 'List<bool>' isn't.
- 'List' is from 'dart:core'.
isSelected: isSelected,
^
您能否建议,我该如何解决这个问题?任何帮助表示赞赏。
谢谢
解决方案
而不是late List<bool> isSelected;
像这样初始化它List<bool> isSelected = [true, false];
推荐阅读
- azure - 应用之间的 Azure AD B2C SSO
- python - 当值为 json 字符串时,python json.loads() 失败
- reactjs - React 中的单元测试入口文件
- css - CSS Spinner Not Spinning
- r - 用 NA 替换字符(0)
- homebrew - 在地窖中安装 Postgres 9.6
- mysql - Azure MySql 休息 API 。插入新的防火墙规则
- xamarin.forms - VS2017 15.7.3 不再打断点 Xamarin.Forms
- bash - 如何根据行文件的随机列表从文件中删除行
- python - Tensorflow while_loop 帧连接