Created
May 23, 2025 08:46
-
-
Save skurfuerst/c99cdde56d55cd1ac878cbaf2b5418c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////// | |
// To run this gist, run: https://dartpad.dev/?id=c99cdde56d55cd1ac878cbaf2b5418c7 | |
////////////////////////////////// | |
import 'package:riverpod/riverpod.dart'; | |
/// A Provider that reads a json file and decodes it into a [Configuration]. | |
final authProvider = FutureProvider<String>((_) async { | |
print("executed authProvider"); | |
return "my-auth-state"; | |
}); | |
final challengeListRepository = | |
AsyncNotifierProvider<ChallengeListRepositoryNotifier, List<String>>( | |
ChallengeListRepositoryNotifier.new, | |
); | |
class ChallengeListRepositoryNotifier extends AsyncNotifier<List<String>> { | |
void add(String listEl) async { | |
print("AJAX Call to create list element (awaited)"); | |
await Future.delayed(Duration(seconds: 2)); | |
print("Invalidating..."); | |
// Reload my data -> this sets "isLoading=true" on the async value | |
// -> good for loading indicator. | |
// -> when build() has completed again (=new data was fetched), isLoading=false again. | |
ref.invalidateSelf(); | |
} | |
@override | |
Future<List<String>> build() async { | |
print("-> DOING AJAX CALL (faked)"); | |
await Future.delayed(Duration(seconds: 2)); | |
print("<- received response"); | |
return ["CHallenge 1", "Challenge 2 " + DateTime.now().toIso8601String()]; | |
} | |
} | |
Future<void> main() async { | |
// Where the state of our providers will be stored. | |
// Avoid making this a global variable, for testability purposes. | |
// If you are using Flutter, you do not need this. | |
final ref = ProviderContainer(); | |
// Initial fetch | |
ref.listen(challengeListRepository, (prev, next) { | |
print ("Loading: " + next.isLoading.toString()); | |
// TODO: WHY DOES THIS NOT PRINT? (the "doing ajax call" above prints") | |
if (next.value == null) { | |
print("Challenge is still null: "); | |
} | |
print("Received challenge list: " + next.value!.join(", ")); | |
}); | |
// do a change | |
ref.read(challengeListRepository.notifier).add("My new element"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment