flutter - Flutter 初始化文件
问题描述
我做了一个颤振应用程序,其中用户可以从 2 张卡片中选择性别,Android Studio 的 GestureDetector 没有给我任何错误,但是当我尝试测试应用程序时,我在设备 LateInitilizationError: Field 'selectedGender 上收到此错误' 尚未初始化
这是代码
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'icon_content.dart';
import 'reusable_card.dart';
const bottomContainerHeight = 80.0;
const activeCardColour = Color(0xFF1D1E33);
const inactiveCardColour = Color(0xFF111328);
const bottomContainerColour = Color(0XFFEB1555);
class InputPage extends StatefulWidget {
InputPage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_InputPageState createState() => _InputPageState();
}
enum Gender {
male,
female,
}
class _InputPageState extends State<InputPage> {
late Gender selectedGender;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: ReusableCard(
onPress: () {
setState(() {
selectedGender = Gender.male;
});
},
colour: selectedGender == Gender.male
? activeCardColour
: inactiveCardColour,
cardChild: IconContent(
customIcon: FontAwesomeIcons.mars,
customText: ('MALE'),
),
),
),
Expanded(
child: ReusableCard(
onPress: () {
setState(() {
selectedGender = Gender.female;
});
},
colour: selectedGender == Gender.female
? activeCardColour
: inactiveCardColour,
cardChild: IconContent(
customIcon: FontAwesomeIcons.venus,
customText: ('FEMALE'),
),
),
),
],
)),
Expanded(
child: ReusableCard(
onPress: () {
setState(() {});
},
colour: activeCardColour,
cardChild: Column()),
),
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: ReusableCard(
onPress: () {
setState(() {});
},
colour: activeCardColour,
cardChild: Column()),
),
Expanded(
child: ReusableCard(
onPress: () {
setState(() {});
},
colour: activeCardColour,
cardChild: Column())),
],
)),
Container(
color: bottomContainerColour,
margin: EdgeInsets.only(top: 10.0),
width: double.infinity,
height: bottomContainerHeight,
),
],
));
}
}
我尝试按照设备告诉我的方式搜索有关颤振文档测试错误的信息,但我找到了解决方案
解决方案
late
只是说这个值会在读取之前被初始化。处理这种情况的简单方法是使 nullable
Gender? selectedGender;
你的其余代码就可以了。
更多关于后期变量
测试小部件
class _APPState extends State<APP> {
Gender? selectedGender;
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(
builder: (context, constraints) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: () {
setState(() {
selectedGender = Gender.female;
});
},
child: Container(
height: 100,
width: 100,
color: selectedGender == Gender.female
? Colors.green
// activeCardColour
: inactiveCardColour,
),
),
GestureDetector(
onTap: () {
setState(() {
selectedGender = Gender.male;
});
},
child: Container(
height: 100,
width: 100,
color: selectedGender == Gender.male
? Colors.green
// activeCardColour
: inactiveCardColour,
),
),
],
),
),
),
);
}
}
推荐阅读
- git - 如何自动暂存所有 Intellj Idea 文件?
- python - 如何将任何对象转换为字节数组并将其保存在内存中?
- mongodb - Connexion 拒绝和 CrashLoopBackOff 使用 Docker GraphQl 为容器运行循环
- perl - 如何在文件中搜索包含 Perl 中关键字的最后一个连续行块
- python-3.x - 使用 scipy.io python 运行 .mat 文件时出现错误“缓冲区对于请求的数组来说太小”
- event-handling - 在 Azure 事件网格中确认事件传递
- pyspark - Pyspark:使用新模式读取 ORC 文件
- python - Django更改密码问题
- android - 如何仅使用 PEM 证书从 AAB 生成签名的 APK?
- c# - 为什么窗口会显示一会儿?