简体   繁体   English

Flutter-为什么我看不到下拉列表中的数据

[英]Flutter- Why I can't see the data inside the Dropdown

I'm new to Flutter and I can't understand why I can't see the data inside the DropDownButton even tho the lists that I use to create the DropDownButton have the data inside them.我是 Flutter 的新手,我不明白为什么我看不到 DropDownButton 中的数据,即使我用来创建 DropDownButton 的列表中有数据。 Can anyone explain to me why is this happening?谁能向我解释为什么会发生这种情况? The only clue I have is that the DropDownButton starts the creation before I create the list from which is taking the data.我唯一的线索是 DropDownButton 在我创建从中获取数据的列表之前开始创建。

The problem is that my dropdown list is empty.问题是我的下拉列表是空的。

I provide the entire code below.我在下面提供了完整的代码。

Code :代码 :

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

To fix this problem I had to change to things :为了解决这个问题,我不得不改变事情:

  1. Thing: I had to change the List from this List listNameOfClients = [];事情:我不得不从这个列表 listNameOfClients = []; 更改列表to this List listNameOfClients = [''];到这个列表 listNameOfClients = [''];

  2. I had to integrate the setState and inside to call the functions.我必须整合 setState 和 inside 来调用函数。

    setState(() { getClientsName(); createListWithClientName(); }); setState(() { getClientsName(); createListWithClientName(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM