Giter Club home page Giter Club logo

Comments (5)

davidmartos96 avatar davidmartos96 commented on July 22, 2024 1

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.

computervirus91 avatar computervirus91 commented on July 22, 2024 1

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.

rrousselGit avatar rrousselGit commented on July 22, 2024

Please share a complete example. Otherwise I can only assume that this is your mistake

from flutter_hooks.

computervirus91 avatar computervirus91 commented on July 22, 2024
// 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.

rrousselGit avatar rrousselGit commented on July 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.