【问题标题】:Flutter initialization filed颤振初始化失败
【发布时间】:2021-11-15 15:25:46
【问题描述】:

我做了一个颤振应用程序,其中用户可以从 2 张卡片中选择性别,Android Studio 的 GestureDetector 没有给我任何错误,但是当我尝试测试应用程序时,我在设备上收到此错误 LateInitilizationError: 字段 '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,
            ),
          ],
        ));
  }
}

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

【问题讨论】:

    标签: flutter dart dart-null-safety


    【解决方案1】:

    late 只是说这个值会在读取之前被初始化。处理这种情况的简单方法是使 nullable

    Gender? selectedGender; 其余代码都可以使用。

    更多关于late-variables

    测试小部件

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

    【讨论】:

    • 但是卡没有改变:(代码不能按我的需要工作
    • 当你从新闻中选择一个性别时它会改变,否则它们都不会被选中并且会有inactiveCardColour。敲一个就行吗?
    • Nope don't work ic an select nothing
    • activeCardColourinactiveCardColour 完全一样,我已经用我的小部件进行了测试,我应该也包括它吗?
    • 我有 40 个错误编译列表访问隐藏方法 Lsun/misc/Unsafe;->getUnsafe()Lsun/misc/Unsafe; (greylist,core-platform-api, linking, allowed) W/.bmi_calculato(17354): 访问隐藏方法 Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform -api, 链接, 允许) W/.bmi_calculato(17354): 访问隐藏方法 Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist , 链接, 允许) W/.bmi_calculato(17354): 访问隐藏方法 Lsun/misc/Unsafe;-.. ...'!_needsLayout': 不正确。
    【解决方案2】:

    从你的第一行开始,你就有了这个

    colour: selectedGender == Gender.male
            ? activeCardColour
            : inactiveCardColour,
    

    这里selectedGender 的值在首次启动时为空。您需要为此属性设置一个值。

    【讨论】:

    • 很抱歉,当应用程序启动时我没有选择......不可能吗?
    • 那你需要通过(selectedGender ?? Gender.male) == Gender.male检查selectedGender是否为空
    • 不工作,所以我有一个新的变量类型 bool
    猜你喜欢
    • 2022-06-19
    • 2021-12-13
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2021-06-06
    • 2018-08-09
    相关资源
    最近更新 更多