icemanbsi / searchable_dropdown

MIT License
107 stars 166 forks source link

Error if removing from `items` the currently selected `value` #24

Closed JChrist closed 4 years ago

JChrist commented 4 years ago

If attempting to remove from the items the currently selected value, a RangeError will be thrown. A very small app to demonstrate this is the following. If you select the last item and then attempt to remove it from the items (clicking the remove last item button) the RangeError will be thrown.

import 'package:flutter/material.dart';
import 'package:searchable_dropdown/searchable_dropdown.dart';

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

class App extends StatefulWidget {
  App();

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

class AppState extends State<App> {
  List<String> _items = ['1', '2', '3'];
  String _selected = '3';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(title: 'TestApp',
        home: Material(child: Column(
          children: <Widget>[
            RaisedButton(child: Text('Remove last Item'),onPressed: () => setState(() => _items.removeLast())),
            SearchableDropdown(items: _items.map((e) => DropdownMenuItem(value: e, child: Text(e))).toList(),
            value: _selected, onChanged: (e) => setState(() => _selected = e))
          ],
        )
      )
    );
  }
}
lcuis commented 4 years ago

Hello @JChrist ,

It seems you solved your issue, is that right?

JChrist commented 4 years ago

Hi @lcuis, yes. I encountered some further issues that hindered the usage of this plugin (e.g. inline dialog breaking in web build) and also needed a way to load further values via api, so I've turned to a custom-made widget. Thanks!

lcuis commented 4 years ago

Great if you found a solution. Cheers!