Created
October 12, 2023 11:15
-
-
Save sue71/3149de354ea313412e2de0aed099df16 to your computer and use it in GitHub Desktop.
useProvider for v2
This file contains 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
import 'package:flutter_hooks/flutter_hooks.dart'; | |
import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
T useProvider<T>(ProviderListenable<T> provider) { | |
final context = useContext(); | |
final container = ProviderScope.containerOf(context); | |
final result = useState<T?>( | |
container.read(provider), | |
); | |
useEffect( | |
() { | |
final sub = container.listen<T>(provider, (T? value, _) { | |
result.value = value; | |
}); | |
return sub.close; | |
}, | |
[container, provider], | |
); | |
return result.value as T; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment