Comments (5)
Sounds like you are recreating the Future/Stream in every build. What you need to do is cache it between builds, with a useMemoized for example
final future = useMemoized(() => api.fetch(), []);
final snap = useFuture(future);
...
from flutter_hooks.
This is nonintuitive. Also, useStream documentation doesn't mention/recommend using useMemoized.
This can create unexpected bugs and/or suboptimal performance for users. For me, using useMemoized with useFuture/useStream is not an acceptable solution.
Anyways, thank you for your response.
from flutter_hooks.
Please share a complete example. Otherwise I can only assume that this is your mistake
from flutter_hooks.
// useFuture example
class PageOne extends HookWidget {
const PageOne({super.key});
@override
Widget build(BuildContext context) {
final snapshot = useFuture<String>(Future.delayed(
const Duration(milliseconds: 500),
() => 'complete',
));
print('################# use future build');
return Scaffold(
appBar: AppBar(title: const Text('use future')),
body: Center(child: Text(snapshot.data ?? 'in progress')),
);
}
}
// useStream example
Stream<int> generateRange(int start, int finish) async* {
for (int i = start; i <= finish; i++) {
yield await Future.delayed(const Duration(milliseconds: 500), () => i);
}
}
class PageTwo extends HookWidget {
final int start;
final int finish;
const PageTwo({
super.key,
required this.start,
required this.finish,
});
@override
Widget build(BuildContext context) {
final snapshot = useStream<int>(generateRange(start, finish));
print('################# use stream build');
return Scaffold(
appBar: AppBar(title: const Text('use stream')),
body: Center(child: Text(snapshot.data.toString())),
);
}
}
from flutter_hooks.
Indeed, like mentioned by David above, you need to cache your Future/Stream in a useMemoized if you want to do that.
If the future/stream passed to useFuture/useStream changes, they start listening to it. So you re-enter a "loading" cycle. It's not a hooks bug :)
from flutter_hooks.
Related Issues (20)
- flutter_hooks: ^0.20.3 HOT 2
- Add hook to observe `didChangeDependencies`
- Why list not updating? HOT 7
- EditText reverting to previous value HOT 2
- Implement useTabControllerList for List<TabController> HOT 6
- Clarify usage of useFuture through documentation HOT 1
- Inconsistency in AppLifecycleState Detection Between iOS and Android in Flutter Hooks HOT 5
- Possibility of deprecating useIsMounted HOT 2
- `when` for `AsyncSnapshot` HOT 1
- Assertion Error with useExpansionTileController HOT 5
- a hook for the DraggableScrollableController HOT 2
- Setting TextEditingController.text selects input in TextField in Web HOT 1
- onAttach and onDetach is missing on usePageController HOT 2
- ERROR: Undefined class 'WidgetStatesController'. HOT 4
- [Feature Request] Is it possible to implement `useSWR` in flutter_hooks HOT 2
- Security issue in Photo Manager HOT 1
- A ticker created by `useSingleTickerProvider` is not muted properly
- Add hook "useFixedExtentScrollController" HOT 1
- state.value = newIndex is not causing a re-build of the widget even though newIndex is different HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_hooks.