4

I want to have a RefreshIndicator that has as a child a StreamBuilder that builds a list, but, it doesn´t work. This is my code:

return Scaffold(
        appBar: buildAppBar(context),
        body:RefreshIndicator(
                  onRefresh: _refresh,
                  child:StreamBuilder(
            stream: list.stream,
            builder: (context, snapshot) {
              return buildListView(snapshot)
}


Future<Null> _refresh() async {
    loadList.sink.add(x);
    return null;
  }
Little Monkey
  • 5,395
  • 14
  • 45
  • 83

1 Answers1

0

RefreshIndicator only appears if its descendant is scrollable. What you can do here is add a ListView on buildListView().

Here's a demo.

Demo

Sample code.

import 'dart:async';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  _increment() {
    bloc.updateIncrement();
  }

  @override
  void dispose() {
    super.dispose();
    bloc.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<int>(
      stream: bloc.incrementCounter,
      builder: (context, AsyncSnapshot<int> snapshot) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: RefreshIndicator(
            onRefresh: () async => _increment(),
            child: ListView.builder(
              // if snapshot data is null, set to 0
              itemCount: snapshot.data ?? 0,
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  child: SizedBox(
                    height: 64,
                    child: Text('Item $index'),
                  ),
                );
              },
            ),
          ),
        );
      },
    );
  }
}

class Bloc {
  int counter = 0;
  final _incrementFetcher = StreamController<int>.broadcast();
  Stream<int> get incrementCounter => _incrementFetcher.stream;

  updateIncrement() {
    _incrementFetcher.sink.add(counter+=1);
  }

  dispose() {
    _incrementFetcher.close();
  }
}

final bloc = Bloc();

Omatt
  • 8,564
  • 2
  • 42
  • 144