How to get data from the FutureProvider in flutter

204    9 months ago

I'm trying to implement local database support in my flutter app which is being managed using Provider, now I want to make the retrieving of data obey the state management pattern, but I've been failing to.

I've tried to make a traditional Provider to achieve this but the app got stuck in a loop of requests to the database, so after some search I found the FutureProvider, but I cant find how can I get a snapshot from the data being loaded

class _ReceiptsRouteState extends State<ReceiptsRoute> {
  List<Receipt> receipts = [];

  Widget build(BuildContext context) {
    return FutureProvider(
      initialData: List(),
      builder: (_){
        return DBProvider().receipts().then((result) {
          receipts = result;
      child: Scaffold(
        appBar: AppBar(
          title: Text(AppLocalizations.of(context).history),
        body: Container(
          child: ListView.builder(
            itemBuilder: (context, position) {
              final item = receipts[position];
              return ListTile(
                title: Text(,

now my app is running as I want but not as how it should run, I used FutureBuilder to get the data from the database directly but I know it should come through the provider so I want to make it right

Answers { 1 }
9 months ago

FutureProvider exposes the result of the Future returned by builder to its descendants.

As such, using the following FutureProvider:

  initialData: 0,
  builder: (_) => Future.value(42),
  child: ...

it is possible to obtain the current value through:



  builder: (context, value, __) {
    return Text(value.toString());