Flutter conditional list

Viewed 36

I want to show list of data when i choose from dropdown, but still error LateInitializationError: Field 'listTanaman' has not been initialized. i just initialize the variable and don't assign value to the variable.

i'm initialize the varibale like this

List<Komoditas>? listTanaman;

and assign in this dropdown

DropdownButton(
    value: dropdownValue,
    items: snapshot.data!.docs
        .map((DocumentSnapshot doc) {
      return DropdownMenuItem(
        value: doc.id,
        child: Container(
          decoration: BoxDecoration(
              borderRadius:
                  BorderRadius.circular(5)),
          height: 100,
          padding: const EdgeInsets.all(10),
          child: Text(doc.get('nama')),
        ),
      );
    }).toList(),
    onChanged: (newValue) {
      setState(() {
        dropdownValue = newValue.toString();
        kategori = snapshot.data!.docs
            .where((newValue) => true)
            .first
            .get('nama');
      });
      db
          .collection('kategori')
          .doc(dropdownValue)
          .collection(kategori!)
          .snapshots()
          .map((event) => listTanaman);
    }),

enter image description here

anyone know where is my fault ?

1 Answers

If you need to check if something is initialized, you should be using a nullable variable instead of Late

change this:

Late String Listkategori;//or
Late List Listkategori;

To

String? Listkategori;
List<urtype> Listkategori = [];
Related