I'm self taught (i.e. not good) and trying to build a flutter page whereby I select a category from a dropdown (_careCat) e.g. "Option 1", then populate a second dropdown with the options for "Option 1" from sqlite. This part works fine.
However, the data I retrieve for the second dropdown has 5 columns of data & by selecting a value on the dropdown I want to be able to set variables related to that data (dropdown has dropdownText but I want to reference fullText). I either seem to be able to get the dropdown menu value to update to the selection but not be able to reference the other data or I'm able to reference the alternative data but not update the selection, the below is the current stage I'm at and I get the error:
"type 'CareAdviceOptions' is not a subtype of type 'String'"
I hope that makes sense. Any pointers would be appreciated, thank you.
care_advice_model.dart
class CareAdviceOptions {
int id;
String category;
String shortCode;
String dropdownText;
String fullText;
CareAdviceOptions({
this.id,
this.category,
this.shortCode,
this.dropdownText,
this.fullText
});
Map<String, dynamic> toMap() => {
'id': id,
'category': category,
'shortCode': shortCode,
'dropdownText': dropdownText,
'fullText': fullText,
};
factory CareAdviceOptions.fromJson(Map<String, dynamic> parsedJson) {
return CareAdviceOptions(
id: parsedJson['id'],
category: parsedJson['category'],
shortCode: parsedJson['shortCode'],
dropdownText: parsedJson['dropdownText'],
fullText: parsedJson['fullText'],
);
}
}
db_provider.dart
await db.execute('CREATE TABLE CareAdviceLookup('
'id INTEGER PRIMARY KEY,'
'category TEXT,'
'shortCode TEXT,'
'dropdownText TEXT,'
'fullText TEXT'
')');
Future<List<CareAdviceOptions>> getCareAdviceMenu(String category) async {
final db = await database;
var response = await db.rawQuery("SELECT * from CareAdviceLookup WHERE category = (?) Order by shortCode asc", [category]);
List<CareAdviceOptions> list = await response.map((c) => CareAdviceOptions.fromJson(c)).toList();
return list;
}
care_advice_page.dart
CareAdviceOptions _care;
Container careOptions() {
return Container(
child: FutureBuilder<List<CareAdviceOptions>>(
future:DBProvider.db.getCareAdviceMenu(_careCat),
builder: (BuildContext context, AsyncSnapshot <List<CareAdviceOptions>>snapshot) {
return snapshot.hasData
? Container(
child: DropdownButton<CareAdviceOptions>(
value: _care,
hint: Text(_care ?? 'Make a selection'),
items: snapshot.data.map((CareAdviceOptions item) { return new DropdownMenuItem<CareAdviceOptions>(
value: item,
child: Text('${item.dropdownText}'),);}).toList(),
onChanged: (CareAdviceOptions value) {
_care = value;
var _index = snapshot.data.indexOf(value);
_careInstruction = snapshot.data[_index].fullText;
_careInsSetAs = snapshot.data[_index].dropdownText;
setState(() {
_isOptionSelected = true;
});
},
),
)
: Container(
child: Center(
child: Container(),
),
);
},
),
);
}
hint: Text(_care ?? 'Make a selection'),. When_careis not null, it tries to display it as a text, so it tries to convert it to a String, and gives you an error. Tryhint: Text(_care.dropdownText ?? 'Make a selection'),hint: _care != null ? Text(_care.dropdownText) : Text('Make a selection') ,