首页 > 解决方案 > 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,
            ),
          ],
        ));
  }
}

我尝试按照设备告诉我的方式搜索有关颤振文档测试错误的信息,但我找到了解决方案

标签: flutterdartdart-null-safety

解决方案


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,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}


推荐阅读