title: RiverPod Maintaining selected item of list
body: <p>I am new to riverpod and trying to figure out a state management issue.</p>
  <p>I have one list, and two independent states which need access to the list. The states also need to select an element of the list and know what its index is. The list can totally change (add or remove elements) and the states need to determine if their selected element is still in the list and update the index accordingly (to 0 if it is not found)</p>
  <p>Here is an example with Riverpod in pure dart:</p>
  <pre><code>import 'package:riverpod/riverpod.dart';
  
  void main(List&lt;String&gt; arguments) {
    final container = ProviderContainer();
    List&lt;String&gt; names = [&quot;Jack&quot;, &quot;Adam&quot;, &quot;Sally&quot;];
    container.read(nameListProvider.notifier).setNames(names);
    //selectedName1 = &quot;Adam&quot;
    //selectedIndex1 = 1
    //selectedName2 = &quot;Sally&quot;
    //selectedIndex2 = 2
    names.remove('Adam');
    container.read(nameListProvider.notifier).setNames(names);
    // print(selectedName1) = &quot;Jack&quot; // set to 0 because selection was removed
    // print(selectedIndex1) = 0
    // print(selectedName2) = &quot;Sally&quot;
    // print(selectedIndex2) = 1 // decrement to match Sally's new list index
  }
  
  final nameListProvider =
      StateNotifierProvider&lt;NameListNotifier, List&lt;String&gt;&gt;((ref) {
    return NameListNotifier();
  });
  
  class NameListNotifier extends StateNotifier&lt;List&lt;String&gt;&gt; {
    NameListNotifier() : super([]);
  
    setNames(List&lt;String&gt; names) {
      state = names;
    }
  }
  </code></pre>
  <p>But I need the selected Name and Index to be Providers</p>
  <p>Update: Here is my more elegant solution:</p>
  <pre><code>import 'package:riverpod/riverpod.dart';
  
  void main(List&lt;String&gt; arguments) {
    final container = ProviderContainer();
    List&lt;String&gt; names = [&quot;Jack&quot;, &quot;Adam&quot;, &quot;Sally&quot;];
    print(container.read(nameListProvider));
    container.read(nameListProvider.notifier).setNames(names);
    var first = container.read(selectionProvider(1).notifier);
    first.setName(1);
    print(container.read(selectionProvider(1)).name);
    var second = container.read(selectionProvider(2).notifier);
    second.setName(2);
    print(container.read(selectionProvider(2)).name);
    names.remove('Adam');
    List&lt;String&gt; newNames = List.from(names);
    container.read(nameListProvider.notifier).setNames(newNames);
    print(container.read(selectionProvider(1)).name);
    print(container.read(selectionProvider(1)).index);
    print(container.read(selectionProvider(2)).name);
    print(container.read(selectionProvider(2)).index);
    print(container.read(nameListProvider));
  }
  
  
  final selectionProvider =
      StateNotifierProvider.family&lt;SelectionNotifier, Selection, int&gt;(
          (ref, page) {
    return SelectionNotifier(ref.read);
  });
  
  class SelectionNotifier extends StateNotifier&lt;Selection&gt; {
    Reader _read;
    SelectionNotifier(this._read) : super(Selection());
    update() {
      final String? selectedName = state.name;
      final List&lt;String&gt; names = _read(nameListProvider);
      if (names == []) {
        state = Selection();
        return null;
      }
      if (selectedName == null) {
        state = Selection(name: names[0], index: 0);
        return;
      }
      int i = names.indexOf(selectedName);
      if (i == -1) {
        state = Selection(name: names[0], index: 0);
        return;
      }
      state = Selection(name: selectedName, index: i);
      return;
    }
  
    setName(int index) {
      final List&lt;String&gt; names = _read(nameListProvider);
      state = Selection(name: names[index], index: index);
    }
  }
  
  final nameListProvider =
      StateNotifierProvider&lt;NameListNotifier, List&lt;String&gt;&gt;((ref) {
    return NameListNotifier(ref.read);
  });
  
  class NameListNotifier extends StateNotifier&lt;List&lt;String&gt;&gt; {
    Reader _read;
    NameListNotifier(this._read) : super([]);
  
    setNames(List&lt;String&gt; names) {
      state = names;
      _read(selectionProvider(0).notifier).update();
      _read(selectionProvider(1).notifier).update();
    }
  }
  
  class Selection {
    final String? name;
    final int? index;
    Selection({this.name, this.index});
  }
  </code></pre>