无法从DropdownButton

时间:2020-07-20 21:26:42

标签: flutter

我遇到了问题,当我显示列表并尝试选择一个项目时,它什么也没选择,但是,它正在打印ID,但没有显示我在屏幕上选择的项目的名称

这是我正在使用的代码。

                         DropdownButton(
                                value: null,
                                isDense: true,
                                hint: new Text("Seleccione Tipo Documento"),
                                items: dataTipoDoc.map((item) {
                                  return new DropdownMenuItem(
                                    child: new Text(item['nom']),
                                    value: item['id'].toString(),
                                  );
                                }).toList(),
                                onChanged: (newVal) {
                                  setState(() {
                                    _mySelectionTipoDoc = newVal;
                                  });
                                },
                                //value: _mySelection,
                              )

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以在下面复制粘贴运行完整代码
您可以将valuenull更改为_mySelectionTipoDoc
代码段

DropdownButton(
      value: _mySelectionTipoDoc,
      isDense: true,
      hint: Text("Seleccione Tipo Documento"),
      items: dataTipoDoc.map((item) {
        return DropdownMenuItem(
          child: Text(item['nom']),
          value: item['id'].toString(),
        );
      }).toList(),
      onChanged: (Val) {
        setState(() {
          _mySelectionTipoDoc = Val;
          print(_mySelectionTipoDoc);
        });
      },          
    ),

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _mySelectionTipoDoc;
  List<Map> dataTipoDoc = [
    {"nom": "1", "id": "id 1"},
    {"nom": "2", "id": "id 2"},
    {"nom": "3", "id": "id 3"}
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropdownButton(
              value: _mySelectionTipoDoc,
              isDense: true,
              hint: Text("Seleccione Tipo Documento"),
              items: dataTipoDoc.map((item) {
                return DropdownMenuItem(
                  child: Text(item['nom']),
                  value: item['id'].toString(),
                );
              }).toList(),
              onChanged: (Val) {
                setState(() {
                  _mySelectionTipoDoc = Val;
                  print(_mySelectionTipoDoc);
                });
              },
              //value: _mySelection,
            ),
          ],
        ),
      ),
    );
  }
}