问题
I have database sqlite data and i want to show my data in drop down with changed id of my rows in table because in future i want to create another drop down to change to value of first drop down anyone can help ?
回答1:
Working with SQLite on flutter
To gather the data from a SQLite database you could use the sqflite plugin (independently if is an iOS or Android device). You have to add the dependency to your pubspec.yaml
.
dependencies:
...
sqflite: any
When you want to use sqflite you have to import the library.
import 'package:sqflite/sqflite.dart';
Next, you have to open a connection to SQLite, here we create a table in case you didn't have one
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});
You can insert or retrieve data using database.rawQuery
.
Insert:
int primaryKeyInsertedRow = await database.rawQuery('INSERT INTO Test(name, value, num) VALUES("some name", 1234, 456.789)');
Select:
List<Map> list = await database.rawQuery('SELECT * FROM Test');
Remember to close the database when you are done with it.
await database.close();
Displaying a drop down menu list
For displaying the data you retrieve first you have to create a class that extends StatefulWidget
, override the createState()
method, and set your own state (in this example, SettingWidgetState
)
@override
_SettingsWidgetState createState() => new _SettingsWidgetState();
Second you should define a state for it, defining a class that extends State<NameOfYourWidget>
. In that class you should have a list of DropdownMenuItem<String>
and a string member of the current selected element.
For the sake of convenience, in this example we are going to use a static list of cities:
List _cities = [
"Cluj-Napoca",
"Bucuresti",
"Timisoara",
"Brasov",
"Constanta"
];
Next, we override initState()
setting our list of DropDownMenuItem
to our list and the currently selected list element. After that we should call super.initState()
.
Also, we need to override the build()
method. The goal is to return a Container
that contains a DropDownButton
, and that DropDownButton
has assigned the list of items (defined in the class), the selected element (also defined in the class) and a event handler for the onChanged:
property (here also are inserted additional widgets with the purpose of making it look nice)
@override
Widget build(BuildContext context) {
return new Container(
color: Colors.white,
child: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text("Please choose your city: "),
new Container(
padding: new EdgeInsets.all(16.0),
),
new DropdownButton(
value: _currentCity,
items: _dropDownMenuItems,
onChanged: changedDropDownItem,
)
],
)),
);
}
Lastly we define the method that is going to be called when a new item is selected from the list (changeDropDownItem(string selectedCity)
in our example).
void changedDropDownItem(String selectedCity) {
setState(() {
_currentCity = selectedCity;
});
}
}
Link where I based my answer for drop down list. You can check out too getting started with sqflite plugin
来源:https://stackoverflow.com/questions/53838685/drop-down-with-sqlite-flutter