BradDoesCode
BradDoesCode

Reputation: 3

Flutter RiverPod notifier not updating my AsyncValue list

I am having trouble with riverPod notifiers and having them update my UI. I have created a list view that can display the notifier build method but when an item is added, it does not update. After searching through the docs and stackoverflow I cannot see where I am going wrong. I have included the full files so you can see if I have missed anything out.

Provider

// joined_festivals.dart
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:findyourevent/models/festival/festival.dart';

part 'joined_festivals.g.dart';

@riverpod
class JoinedFestivals extends _$JoinedFestivals {
  @override
  Future<List<Festival>> build() async {
    return [];
  }

  void addFestival(Festival festival) async {
    final previousState = await future;
    state = AsyncValue.data([...previousState, festival.copyWith()]);
    print(state);
    ref.notifyListeners(); // attempt to notify listeners
  }
}

NOTE: The print on addFestival prints what I expect. The old state + the new festival

List view

//my_schedule.dart
import 'package:hooks_riverpod/hooks_riverpod.dart';

class MySchedule extends ConsumerWidget {
  const MySchedule({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final AsyncValue<List<Festival>> joinedFestivals =
        ref.watch(joinedFestivalsProvider);
    print(joinedFestivals.value);
    return switch (joinedFestivals) {
      AsyncData(:final value) => Padding(
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: ListView.builder(
            shrinkWrap: true,
            padding: const EdgeInsets.only(top: 10),
            itemCount: value.length,
            itemBuilder: (context, index) => FestivalCard(
              openFestival: () => Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) =>
                          FestivalInformation(festival: value[index]))),
              festival: value[index],
            ),
          ),
        ),
      AsyncError() =>
        const Center(child: Text('Oops, something unexpected happened')),
      _ => const Center(child: CircularProgressIndicator()),
    };
  }
}

NOTE: The print always prints the result of build()

Upvotes: 0

Views: 677

Answers (1)

Randal Schwartz
Randal Schwartz

Reputation: 44186

Based on the original comments, you apparently confirmed my suspicion that your provider was not marked "keepAlive", and being recycled when not watched.

Just putting this here so you can mark it as the answer for future queries.

Upvotes: 0

Related Questions