0

I am trying to feed one DropdownButton with a list of strings.

So my code start with :

List<String> list0 = [];

class mainPage extends StatefulWidget {
  const mainPage({super.key});

  @override
  State<mainPage> createState() => _mainPageState();
}
class _mainPageState extends State<mainPage> {
@override
  void initState() {
    list0.add("value1");
    list0.add("value 2");
    super.initState();
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body:Center(
         child: DropdownButton<String>(
              onChanged: (String? value) {
                 setState(() {
                 dropdownValue = value!;
              });
              items: list0.map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
        ),
      ),
    );
  }
}

What I am not understanding is the code are generating an error where say no values for list0. Cant be null.

So if I declarate like List list0 = ["0","1"]; no problens, will work fine. But I want to create an empty list to feed with my dynamic information...

What I am doing wrong guys?

Thank you!

I had tryed to clear the list. Create with one single element and remove it after but off course that is not correct.

guguCara
  • 21
  • 5

1 Answers1

0

It looks like you're referencing a dropdownValue variable without defining it first.

Here's a dart file that I successfully compiled into a Flutter app:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) => const MaterialApp(home: MainPage());
}

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  List<String> list0 = [];
  String dropdownValue = "";
  @override
  void initState() {
    list0.add("value1");
    list0.add("value2");
    dropdownValue = list0.first;
    super.initState();
  }

  DropdownMenuItem<String> item(String value) => DropdownMenuItem<String>(
        value: value,
        child: Text(value),
      );

  @override
  Widget build(BuildContext context) => Scaffold(
        body: Center(
          child: DropdownButton<String>(
            value: dropdownValue,
            onChanged: (String? value) => setState(() => dropdownValue = value!),
            items: [for (final s in list0) item(s)],
          ),
        ),
      );
}
nate-thegrate
  • 378
  • 2
  • 13
  • ow Nate, thank you for your attention! – guguCara Aug 02 '23 at 00:01
  • ow Nate, thank you for your attention! But if I try to replace this lines list0.add("value1"); list0.add("value2"); for one function that will feed the list0 I keep the same problem, no element... I tryed feedList(), where I feed the list with some values, and after it I use print( list0.first); and bum, error. I dont understand because doesnt make sense for me. – guguCara Aug 02 '23 at 00:07
  • ow, I am using a await in my function So this is the problem off course...I will try something around this async fcuntion...Is because I am using json to feed that list...So probably the one chance here is remove the async Future feedNames() async{ – guguCara Aug 02 '23 at 00:21
  • If you need to await a function, you're welcome to try the `FutureBuilder` widget. – nate-thegrate Aug 02 '23 at 02:48