logo
Tags down

shadow

How to Render data from Request inside a FutureBuilder


By : harshad velapure
Date : October 17 2020, 06:10 AM
I think the issue was by ths following , I'm trying to list pokemons from my api, its works when i call the list but when i try to get the data for each pokemon it doesn't render the data. , One way is to create a FutureBuilder per item, like so:
code :
Widget build(BuildContext context) {
  return FutureBuilder<PokeData>(
    future: api.getPokemonList(),
    builder: (
      BuildContext context,
      AsyncSnapshot<PokeData> snapshot
    ) {
      if(snapshot.hasData && snapshot.connectionState == ConnectionState.done) {
        return Container(
          child: ListView(
            children: snapshot.data.results.map((pokemon) {
              FutureBuilder(
                future: api.getPokeDetail(pokemon.url),
                builder: (
                  BuildContext context,
                  AsyncSnapshot snapshot
                ) {
                  if(snapshot.hasData && snapshot.connectionState == ConnectionState.done) {
                    return PokemonWidget(
                      pokemon.name,
                      resp.sprites.normal,
                      resp.types
                    );
                  } else {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }
                }
              )
              }).toList(),
          ),
        );
      } else {
        return Center(
          child: CircularProgressIndicator(),
        );
      }
    },
  );
}


Share : facebook icon twitter icon

Fetch GET request with Reactjs - how do i implement a reusable component within my request to render my data?


By : Harry Shaw
Date : March 29 2020, 07:55 AM
around this issue You should store the result of the api inside the state and render the inside the render function.
Btw it would be better to separate your components:
code :
const BookListing = ({ books }) => (
  <ul>
    {books.map(book => <li key={book.id}>{book.title}</li>)}
  </ul> 
);
class Books extends Component {
  constructor(props) {
    super(props);
    this.state = {books: []};
  }
  componentDidMount() {
    fetch('http://localhost:8000/api/Books')
      .then(data => data.json())
      .then((data) => { this.setState({ books: data }) }); 
  }
  render() {
    return <BookListing books={this.state.books} />;
  }
}

How to use FutureBuilder inside SliverList


By : Robert Bradford
Date : March 29 2020, 07:55 AM
I wish this helpful for you The children of a CustomScrollView must be slivers, you can not use a FutureBuilder.
Instead, rebuild the CustomScrollView when the future completes:
code :
// build fixed items outside of the FutureBuilder for efficiency
final someOtherSliver = SliverToBoxAdapter(...);

return FutureBuilder<List<Data>>(
  future: getQuake(), // this is a code smell. Make sure that the future is NOT recreated when build is called. Create the future in initState instead.
  builder: (context, snapshot){
    Widget newsListSliver;
    if(snapshot.hasData) {
      newsListSliver = SliverList(delegate: SliverChildBuilderDelegate(...))
    } else {
      newsListSliver = SliverToBoxAdapter(child: CircularProgressIndicator(),);
    }

    return CustomScrollView(
      slivers: <Widget>[
        someOtherSliver,
        newsListSliver
      ],
    );
  },
);
return FutureBuilder<..>(
  ...
  builder: (context, snapshot1) {
    return FutureBuilder<..>(
      ...
      builder: (context, snapshot2) {
        return CustomScrollView(...);
      }
    )
  }
)

flutter infinite loop when FutureBuilder inside LayoutBuilder


By : user3008683
Date : March 29 2020, 07:55 AM
it should still fix some issue The documentation for FutureBuilder states that you should not create the future on every build: https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
Either run getPageCount in initState, in a field initializer, or prepare a stateful widget similiar to FutureBuilder that takes a future creating function and calls this function in it's own initState.
code :
class CachingFutureBuilder<T> extends StatefulWidget {
  final Future<T> Function() futureFactory;
  final AsyncWidgetBuilder<T> builder;

  const CachingFutureBuilder(
      {Key key, @required this.futureFactory, @required this.builder})
      : super(key: key);
  @override
  _CachingFutureBuilderState createState() => _CachingFutureBuilderState<T>();
}

class _CachingFutureBuilderState<T> extends State<CachingFutureBuilder<T>> {
  Future<T> _future;

  @override
  void initState() {
    _future = widget.futureFactory();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<T>(
      future: _future,
      builder: widget.builder,
    );
  }
}
class ScrollableTabsDemo extends StatelessWidget {
  Future<int> getPageCount(Size size) {
    return Future.value(1);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: LayoutBuilder(
          builder: (context, constraints) {
            final size = Size(constraints.maxHeight, constraints.maxWidth);
            return CachingFutureBuilder<int>(
              key: ValueKey(size),
              futureFactory: () => getPageCount(size),
              builder: (context, snapshot) {
                return Text(snapshot.toString());
              },
            );
          },
        ),
      ),
    );
  }
}

Using Futurebuilder to render widgets conditionally


By : user3224425
Date : March 29 2020, 07:55 AM
should help you out You are doing it right but the problem is with the returning object from the FutureBuilder, see the comments in this code:
code :
// *NOTE: build return Widget: Widget build...
Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: FutureBuilder(
          future: loadWidget(),
          builder: (BuildContext context, AsyncSnapshot<Widget> widget){
            // But 'widget' here is NOT a widget, it is an AsyncSnapshot object,
            // return widget; // **is wrong**
            // instead:
            return widget.data;

            // and better to make it like this:
            //if (!widget.hasData) {
            //  return Center(
            //    child: CircularProgressIndicator(),
            //  );
            //}
            //return widget.data;
          },
        ),
      ),
    );
  }

How to fix recursing HTTP request in FutureBuilder?


By : user3655681
Date : March 29 2020, 07:55 AM
hop of those help? I'm creating an app that has a list inside a screen. What I want to do is whenever the app makes the HTTP request (getting the data), I want to show CircularProgressIndicator() on the screen. I tried to use a FutureBuilder to implement this, but the app recursively/continuously loading the data (when the ListView is set, the app load the data again and again). Here are some of my code: , CReate AsyncMemoizer in State Class
code :
AsyncMemoizer _memoizer = AsyncMemoizer();
  Future listenForBeers() async {
 return this._memoizer.runOnce(() async {
    final Stream<Beer> stream = await getBeers();
    stream.listen((Beer beer) => setState(() => _beers.add(beer)));

)};
  }



 Future refreshBeers() async {
     _memoizer = AsyncMemoizer();
     return listenForBeers();
      }
Related Posts Related Posts :
  • Error Running Gradle on Flutter with Visual Studio Code
  • StaggeredGridView not scrolling after updating Flutter
  • Provider Throw Errors in InitState. Provider.of<SomeProvider>(context)
  • How to delete/detect any emoji as a whole from a string in Flutter?
  • How to force flutter to use wifi rather than 4G?
  • Dart code reformat didn't give me the expected format
  • PlatformException (PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 12500: , null))
  • Flutter[desktop]: how to get input from physical keyboard without textfield?
  • Flutter: upload image using binary body
  • How to do encryption like android's "PBEWithMD5AndDES" in flutter dart?
  • Why I cant use spread operator in this case?
  • Flutter The const variable 'IconData' must be initialized
  • How to set literals after Dart 2.2
  • Dart_LoadScriptFromKernel: The binary program does not contain 'main'
  • How to set final/const properties in Dart constructor
  • What is the best way to trim a trailing character in Dart?
  • How do I get the AstNode from some Element, in Dart Analyzer / source-gen?
  • Difference between x = -5.abs() and print(x.abs())?
  • Can you specify a List<T> with specific length as a type
  • Is there a Dart function to convert List<int> to Double?
  • How can I check if a string contains a substring, while ignoring case
  • What's the difference between double.toStringAsFixed and toStringAsPrecision in dartlang?
  • Constructor not called on static attribute
  • Function declaration vs lambdas
  • Banker's Rounding in Dart
  • What difference between add and sink.add?
  • dart:html WebSocket vs dart:io WebSocket
  • correct way to import mydart.dart file in main
  • In Dart, how to access a parent private var from a subclass in another file?
  • What is the difference between compile time constant and run time constant
  • Dart "For Loop" "If Else" "Boolean return"
  • Dart symbol literals
  • How do these function return values work in Dart?
  • Dart can't take input
  • Is there a way to add a static extension method on a class directly in Dart?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk