Flutter:StreamBuilder的行为类似于StatelessWidget

时间:2019-01-20 14:21:30

标签: flutter algolia stream-builder

我是编程的初学者,并尝试在Flutter中编写App。我将数据从algolia加载到StreamBuilder(通过将algolia lib与as.Stream()结合使用-否则它将不起作用)。该代码运行良好,但它不会自动刷新数据-我必须在应用程序中加载新屏幕或热重载,然后数据才会更新。看起来StatefulWidget的行为类似于StatelessWidget。您有任何想法-可能在某处包括setState(我认为这对于StreamBuilder而言不是必需的)?感谢您的帮助;-)。

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]),
              );
            });
      },
    );
  }
}

1 个答案:

答案 0 :(得分:0)

我还与Dart的Algolia软件包一起工作,并且在实施方面苦苦挣扎。现在您的问题对我有很大帮助,我想我也可以为您解决问题。您应该改用FutureBuilder。我的代码看起来像这样,与您的代码非常相似,但是应该适合您的情况:)

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) ));
            }
          );
        },
      );}

    }
  )),

);}}