0

I have done a flutter app with a code where the user can select gender from 2 cards and the GestureDetector the android studio don't give me any error but when I try to test the app I recive this error on device LateInitilizationError: Field 'selectedGender' has not been initialized

here is the code

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

i try to search about flutter docs testing erros as device told to me but i find a solution

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
jonnyf
  • 21
  • 6

2 Answers2

1

late is simply saying that this value will be initialized before read. Simple way to handle this situation will be making nullable

Gender? selectedGender; the rest of your code will be ok with it.

More about late-variables

Test Widget


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

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • But so the card don't change :( the code don't work as i need – jonnyf Nov 15 '21 at 16:04
  • It will change while you select a gender from press, else both of them are unselected and will have `inactiveCardColour`. Does it work by tapping one? – Md. Yeasin Sheikh Nov 15 '21 at 16:05
  • Nope don't work ic an select nothing – jonnyf Nov 15 '21 at 16:08
  • `activeCardColour` and `inactiveCardColour` are quite the same, i've tested with my widget, should I include that as well? – Md. Yeasin Sheikh Nov 15 '21 at 16:11
  • i have e list of 40 error copiling Accessing hidden method Lsun/misc/Unsafe;->getUnsafe()Lsun/misc/Unsafe; (greylist,core-platform-api, linking, allowed) W/.bmi_calculato(17354): Accessing hidden method Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform-api, linking, allowed) W/.bmi_calculato(17354): Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed) W/.bmi_calculato(17354): Accessing hidden method Lsun/misc/Unsafe;-.. ...'!_needsLayout': is not true. – jonnyf Nov 15 '21 at 16:15
  • Do a `flutter clean` and restart – Md. Yeasin Sheikh Nov 15 '21 at 16:17
  • I try to do a flutter clean but i see in your code you use onTap i use onPress maybe this is the problem? – jonnyf Nov 15 '21 at 16:21
  • Both are quite the same bro, just one with known by different names for different widget. – Md. Yeasin Sheikh Nov 15 '21 at 16:24
  • I know but i home that's can be the problem becouse after clean i recive this error and still don't work :( W/.bmi_calculato(17769): Accessing hidden method Lsun/misc/Unsafe;->getUnsafe()Lsun/misc/Unsafe; (greylist,core-platform-api, linking, allowed) W/.bmi_calculato(17769): Accessing hidden method Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform-api, linking, allowed) W/.bmi_calculato(17769): Accessing hidden method Lsun/misc/Unsafe;-...... – jonnyf Nov 15 '21 at 16:26
  • also could be issue on different widget tree, you can try using key on `ReusableCard`'s parrent. – Md. Yeasin Sheikh Nov 15 '21 at 16:28
  • I have an dart file for reusable_card but i don't think i can use this there becouse there is not logic part in this file – jonnyf Nov 15 '21 at 16:31
  • From your question, I was able to produce this issue and solved it this way. You might need to create a separate question about new errors, including those snippets that will reproduce the errors. – Md. Yeasin Sheikh Nov 15 '21 at 16:36
0

From your first row you have this

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

here the value of selectedGender is null at first launch. You need to set a value for this property.

cahyo
  • 597
  • 2
  • 5
  • 14