1

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

3
  • 1
    Not sure but it might be on this line : hint: Text(_care ?? 'Make a selection'),. When _care is not null, it tries to display it as a text, so it tries to convert it to a String, and gives you an error. Try hint: Text(_care.dropdownText ?? 'Make a selection'), Commented May 27, 2021 at 15:25
  • Thanks, that could be a pointer... I still got an error but removing this line now gives me the error "There should be exactly one item with [DropdownButton]'s value: Instance of 'CareAdviceOptions'. " Commented May 27, 2021 at 15:40
  • This seemed to fix it although it might be a dirty fix, thanks for the pointer. hint: _care != null ? Text(_care.dropdownText) : Text('Make a selection') , Commented May 28, 2021 at 7:21

1 Answer 1

1

Not sure but it might be on this line : hint: Text(_care ?? 'Make a selection'),. When _care is not null, it tries to display it as a text, so it tries to convert it to a String, and gives you an error. Try hint: Text(_care.dropdownText ?? 'Make a selection'), – BabC 21

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.