0

I am a beginner at programming and try to code an App in Flutter. I load data from algolia to StreamBuilder (by using an algolia lib in combination with as.Stream() - otherwise it doesnt work). The code works well, but it does not automatically refresh the data - I have to load a new screen in the app or hot reload, then the data updates. Looks like the StatefulWidget behaves like a StatelessWidget. Do you have any idea - maybe include setState somewhere (which I thought isn't necessary for StreamBuilder)? Thanks for your help ;-).

import 'package:first_app/group_detail.dart';
import 'package:flutter/material.dart';
import 'package:algolia/algolia.dart';

class First extends StatefulWidget {
  @override
  FirstState createState() {
    return new FirstState();
  }
}

class FirstState extends State<First> {

  static Algolia algolia = Algolia.init(
    applicationId: 'xxx',
    apiKey: 'xxx',
  );

  queryFunc()  {

    AlgoliaQuery query = algolia.instance.index('places).setAroundLatLng('51.5078845,7.4702625');
    Future<AlgoliaQuerySnapshot> snap  = query.getObjects();
    return snap;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return StreamBuilder(
      stream: queryFunc().asStream(),
      builder:
          (BuildContext context, AsyncSnapshot<AlgoliaQuerySnapshot> snapshot) {
        if (!snapshot.hasData) return new Text('Loading...');
        final int documentsLength = snapshot.data.hits.length;

        return new ListView.builder(
            itemCount: documentsLength,
            itemBuilder: (context, int index) {
              final AlgoliaObjectSnapshot document = snapshot.data.hits[index];
              return new ListTile(
                leading: CircleAvatar(
                    child: Text(document.data['name'].substring(0, 1))),
                title: new Text(document.data['name']),
                subtitle: new Text(document.data['text]),
              );
            });
      },
    );
  }
}
Alexandra
  • 79
  • 7
  • Look at this doc https://api.dartlang.org/stable/2.1.0/dart-async/Future/asStream.html It says that asStream will produce single data or error event containing the completion result of this future, and then it will close with a done event. So as far as I can understand your stream will produce data only once. – pblead26 Jan 22 '19 at 10:54
  • @pblead26 do you have an idea to solve this problelm? – Alexandra Jan 22 '19 at 14:05

1 Answers1

0

I am also working with the Algolia package for Dart and where struggling with the implementation. Now your question helped me alot and I think I also can help you with your problem. You should use a FutureBuilder instead. My code looks like this and is very similar to yours, but should work for your case :)

class Test extends StatefulWidget {
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
final Searchalgolia searchalgolia = new Searchalgolia();

@override
Widget build(BuildContext context) {

return Scaffold(
  appBar: AppBar(
    title: Text('test'),
  ),
  body: Container(child: 
  FutureBuilder(
    // perfomrs query in Algolia and returns snap from .getObjects()
    future: searchalgolia.searchAlgolia("joas"),
    builder: (BuildContext context, AsyncSnapshot snapshot){
      if(snapshot.data == null ){
        return LinearProgressIndicator();
      }

      else {
        return ListView.builder(
        itemCount: snapshot.data.hits.length,
        itemBuilder: (BuildContext context, int index) {
          final AlgoliaObjectSnapshot document = snapshot.data.hits[index];
          return ListTile(

            title: Text(document.data['subject']),
            onTap: () {
              Navigator.push(context, 
              MaterialPageRoute(builder: (context) => PatientOverview(snapshot.data[index].id, 
              "AZ" + snapshot.data[index].id) ));
            }
          );
        },
      );}

    }
  )),

);}}
Max Durden
  • 11
  • 1