1

i have a DropDownButton, which is filled from an SQLite DB which is ok for my app for now. But after choosing an entry, the DropDownButton didnt show the choosen entry, just the hint. To check my entry i try to fill a textfield also with the choosen entry, but this isnt changed too. Here is my code for the DropDownButton:

  List<DropdownMenuItem<String>> teamList;
  DropdownMenuItem selectedTeam;

  DropdownButton(
          hint: Text("Choose"),
          value: selectedTeam,
          onChanged: (value) {
            setState(() {
              _teamController.text = value.name;
              selectedTeam = value;
            });
          },
          items: teamList,
        ),

actually i fill my teamList with a codesnippet inside the initstate:

super.initState();

teamList = [];
db.getData().then((listMap) {
  listMap.map((map) {
    print(map.toString());
    return getDropDownWidget(map);
  }).forEach((dropDownMenuItem) {
    teamList.add(dropDownMenuItem);
  });
  setState(() {});
});

and with this:

DropdownMenuItem<String> getDropDownWidget(Map<String, dynamic> map) {
  return DropdownMenuItem<String>(
    value: map['team'],
    child: Text(map['team']),
);

}

in my dbhelper-file i have this code:

Future<List<Map<String, dynamic>>> getData() async {
var dbClient = await db;
return await dbClient.rawQuery('SELECT team FROM teamTable');

}

Thomas B
  • 65
  • 5

1 Answers1

0

Hey Thomas Check out this example :

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SampleApp(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class SampleApp extends StatefulWidget {
  @override
  _SampleAppState createState() => _SampleAppState();
}

class _SampleAppState extends State<SampleApp> {
  List<String> teamList = ['Sample', 'Sample2', 'Sample3', 'Sample4'];
  String selectedTeam;
  TextEditingController _teamController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Your heading'),
        ),
        body: Container(
            child: Column(
          children: <Widget>[
            TextFormField(
              controller: _teamController,
            ),
            new DropdownButton<String>(
              items: teamList.map((String value) {
                return new DropdownMenuItem<String>(
                  value: value,
                  child: new Text(value),
                );
              }).toList(),
              value: selectedTeam,
              hint: Text('Choose'),
              
            
              onChanged: (value) {
                setState(() {
                  _teamController.text = value;
                  selectedTeam = value;
                  print('This is the selected value: $selectedTeam');
                });
              },
            ),
          ],
        )));
  }
}

Let me know if it works.

Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34
  • I try to follow your answer, but i have problems to fill the teamList from SQLite. my code looks like: teamList = []; db.getData().then((listMap) { listMap.map((map) { print(map.toString()); return getDropDownWidget(map); }).forEach((dropDownItem) { teamList.add(dropDownItem); }); setState(() {}); }); now i dont have "dropDownItem" and i cant fiddle out what it should be in your solution – Thomas B Dec 27 '20 at 15:23
  • please add code in the question you asked not in the comments and just from the code that you posted maybe you need await and wait till you fetch the data from the database. – Sagar Acharya Dec 27 '20 at 15:39
  • 1
    till then I will just update my answer and do it using the sqlite database. – Sagar Acharya Dec 27 '20 at 15:40