title: Using a List value index stored in a variable trows an error while using List value directly works fine Flutter
body: <p>In my screen I have on half a screen a <code>ListView</code> that shows all booking records from local db which get stored in a <code>List&lt;Booking&gt; bookings</code>, on the other half I have a detailed view for the selected booking item from the ListView.
  In the detailed view I also have 3 buttons that send push notification event to bloc which takes a booking as a input parameter.
  To be able to have a reference to the Booking displayed in the details view I store the ListView item index in a <code>int selectedBooking = 0;</code> variable when selecting an item from the <code>ListView</code>.
  The problem is that sending the notification event from the buttons using that variable throws an error. I tried sending it directly from <code>ListView</code> item selection and it get sent just fine.
  I'm sure it's not null as I print <code>bookings[selectedBooking]</code> before sending the notification and prints fine, but some how makes the notification sending event throw an error that doesn't get printed in the catch scope. </p>
  
  <p>The only difference is that in the <code>ListView</code> item's <code>onTap:</code> I take the booking directly come the <code>List&lt;Booking&gt;</code> with <code>booking[index]</code> while in the button's <code>onPressed:</code> I use the variable that stores that same index <code>bookings[selectedBooking]</code>.. </p>
  
  <p>This is the screen build:</p>
  
  <pre><code>Widget build(BuildContext context) {
      return MultiBlocProvider(
          providers: [
            BlocProvider&lt;BookingBloc&gt;(
              create: (context) =&gt;
                  BookingBloc(widget.user)..add(LoadBookings(user: widget.user)),
            ),
            BlocProvider&lt;PushNotificationBloc&gt;(
              create: (context) =&gt; PushNotificationBloc(),
            ),
          ],
          child: MultiBlocListener(
            listeners: [
              BlocListener&lt;BookingBloc, BookingState&gt;(
                  listener: (BuildContext context, BookingState state) {
                if (state is LoadedBookings) {
                  setState(() {
                    bookings = state.bookings;
  //                  isSelected = state.isSelected;
  //                  Timer(Duration(milliseconds: 50), () {
  //                    scrollToIndex(bookings);
  //                  });
                  });
                }
                if (state is BookingDetails) {
                  setState(() {
                    bookingId = state.booking.bookingId;
                    bookingDate = dateOnlyFormat.format(
                        DateTime.fromMillisecondsSinceEpoch(
                            state.booking.bookingStart));
                    bookingStart = timeFormat.format(
                        DateTime.fromMillisecondsSinceEpoch(
                            state.booking.bookingStart));
                    bookingEnd = timeFormat.format(
                        DateTime.fromMillisecondsSinceEpoch(
                            state.booking.bookingEnd));
                    customerName = state.booking.customerName;
                    works = state.booking.worksNameList;
                    bookingPrice = state.booking.bookingPrice;
                    collectedOnDate = state.booking.collectedOnDate;
                  });
                }
              }),
            ],
            child: Container(
  </code></pre>
  
  <p>This is the <code>ListView</code> item's onTap:</p>
  
  <pre class="lang-dart prettyprint-override"><code>onTap: () {
    // sending push here does work...
    //BlocProvider.of&lt;PushNotificationBloc&gt;(
    //context).add(
    //BookingStartedPushNotification(
    //booking: bookings[index]));
    if (isSelected[index] == false) {
      //store booking in a variable for sending notifications
      selectedBooking = index;
      print(
        'selected booking is ${bookings[selectedBooking].toMap().toString()}');
      //load selected booking
      BlocProvider.of&lt;BookingBloc&gt;(context).add(
        LoadBookingDetails(
          DateTime.fromMillisecondsSinceEpoch(
            bookings[index].bookingStart),
        ),
      );
    } else {
      setState(() {
        //selectedBooking = null;
        print(
          'selected booking is ${bookings[selectedBooking].toMap().toString()}');
        bookingId = '';
        bookingDate = '';
        bookingStart = '';
        bookingEnd = '';
        customerName = '';
        works = '';
        bookingPrice = '';
        bookingState = '';
        collectedOnDate = '';
      });
    }
  setState(() {
    //invert isSelected value
    isSelected[index] = !isSelected[index];
  }
  </code></pre>
  
  <p>and this is the button onPressed:</p>
  
  <pre class="lang-dart prettyprint-override"><code>onPressed: () {
    print("Started button is been clicked");
    //cache.play(
    //'tableViewClose.mp3');
    print(
      'selected booking is : ${bookings[selectedBooking].toMap().toString()}');
    try {
      BlocProvider.of&lt;PushNotificationBloc&gt;(
        context)
      .add(BookingStartedPushNotification(
        booking:
        bookings[selectedBooking]));
    } catch (e) {
      print('Error is : $e');
    }
  
    BlocProvider.of&lt;BookingBloc&gt;(context).add(
      UpdateBookingState(
      user: widget.user,
      cityDb: widget.cityDb,
      regionDb: widget.regionDb,
      booking:
      bookings[selectedBooking],
      state: 'Started'));
  }
  </code></pre>