Flutter 颤振-为什么我能';我看不到下拉列表中的数据

Flutter 颤振-为什么我能';我看不到下拉列表中的数据,flutter,Flutter,我是个新手,我不明白为什么我看不到DropDownButton中的数据,即使我用来创建DropDownButton的列表中有数据。谁能向我解释一下为什么会这样?我唯一的线索是,下拉按钮在我创建从中获取数据的列表之前开始创建 问题是我的下拉列表是空的 我在下面提供了完整的代码 代码: import 'package:flutter/material.dart'; import 'ListOfClienti.dart'; class CreateClient extends StatefulWi

我是个新手,我不明白为什么我看不到DropDownButton中的数据,即使我用来创建DropDownButton的列表中有数据。谁能向我解释一下为什么会这样?我唯一的线索是,下拉按钮在我创建从中获取数据的列表之前开始创建

问题是我的下拉列表是空的

我在下面提供了完整的代码

代码:

import 'package:flutter/material.dart';

import 'ListOfClienti.dart';

class CreateClient extends StatefulWidget {
  @override
  _CreateClientState createState() => _CreateClientState();
}

class _CreateClientState extends State<CreateClient> {
  String ClientCod = '';
  String currentClient = '';
  List<String> listNameOfClients = [];
  List<DropdownMenuItem<String>> actualList2 = [];

  void getClientsName() {
    for (var i = 0; i <= ListDatabase.length - 1; i++) {
      var name = ListDatabase[i].nome;
      print(name);
      listNameOfClients.add(name);
    }
  }

  void createListWithClientName() {
    for (String oneByOneClient in listNameOfClients) {
      var VariableToInsert2 = DropdownMenuItem(
        child: Text(oneByOneClient),
        value: oneByOneClient,
      );
      actualList2.add(VariableToInsert2);
    }
  }

  //Method that allows me to create the DropDownButton
  DropdownButton<String> createNameMachine() {
    return DropdownButton(
      items: actualList2,
      style: TextStyle(
        color: Colors.black,
        fontFamily: 'Keqima',
        fontSize: 15,
      ),
      value: currentClient,
      onChanged: (clientSelected) {
        setState(() {
          currentClient = clientSelected;
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xff757575),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20),
            topRight: Radius.circular(20),
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(16),
              child: TextField(
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'Inserisci il codice cliente',
                ),
                onChanged: (textInsideTheField) {
                  ClientCod = textInsideTheField;
                },
              ),
            ),
            Padding(
              padding: EdgeInsets.all(16),
              child: Row(
                children: <Widget>[
                  FlatButton(
                    onPressed: () {
                      getClientsName();
                      createListWithClientName();
                      print(actualList2.length);
                    },
                    child: Text(
                      'Clienti : ',
                      style: TextStyle(
                        fontSize: 20,
                        fontFamily: 'Keqima',
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 20,
                  ),
                  createNameMachine(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入'ListOfClienti.dart';
类CreateClient扩展StatefulWidget{
@凌驾
_CreateClientState createState()=>\u CreateClientState();
}
类_CreateClientState扩展状态{
字符串ClientCod='';
字符串currentClient='';
列表ListNameOfClient=[];
列表实际值t2=[];
void getClientsName(){

对于(var i=0;i要解决此问题,我必须更改为:

  • 事情:我不得不将列表从ListNameOfClient=[];更改为ListNameOfClient=[''

  • 我必须集成setState和inside来调用函数

    设置状态(){ getClientsName(); createListWithClientName(); }))


  • 例如,在您的
    getClientsName
    方法中,当它应该是
    .name
    时,您将其命名为
    var name=ListDatabase[i].nome
    。此外,您似乎没有创建
    ListDatabase
    ,除非它在代码中的其他地方没有显示,否则它必须是.这不是问题。是的,正如你所看到的,我导入了ListofClient,在里面我有ListDatabase。这两个方法做得很好。创建下拉按钮的3个方法是问题所在