刷新小部件时,Stream.asFuture()复制数据

时间:2020-09-11 10:53:42

标签: firebase flutter dart google-cloud-firestore

当我第一次加载小部件时,它给了我一个错误,但按预期工作。当我转到另一个屏幕时,尽管它会复制将来的数据,但是返回。

@override
  Widget build(BuildContext context) {
   

    final userdata = context.watch<UserDataNotifier>();


    UserData data = Provider.of<UserData>(context);

    double h = MediaQuery.of(context).size.height;

    // print(data);
    return StreamBuilder(
      stream: Stream.fromFuture(data.getTheUserClasses),
      builder: (context, snapshot) {
        snapshot.data.toString();
        return Scaffold(
          backgroundColor: Color(0xff3DDC97),
          appBar: AppBar(
            backgroundColor: Color(0xff7211E0),
            title: userdata.user == null
                ? CircularProgressIndicator()
                : Text(userdata.user.firstName ?? ""),
          ),
          body: Container(
              // padding: EdgeInsets.symmetric(vertical: h / 8),
              padding: EdgeInsets.fromLTRB(0, h / 8, 0, 0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  SizedBox(
                      height: h * .5,
                      child: data.classList == null
                          ? Loading()
                          : data.classList.isEmpty
                              ? Loading()
                              : UserClassList(
                                  data: data.classList,
                                )),
                  RaisedButton(
                    child: Text("Add Class"),
                    onPressed: () {
                      Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => PickFromAllClasses()))
                          .then((value) => value ? _refresh() : null);
                    },
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20)),
                    color: Colors.blue,
                  )
                ],
              )),
        );
      },
    );
  }

这是我获取数据的方式

    class UserData {
  String uid;
  String firstName;
  int rating;
  List<String> classes;
  List<ClassData> classList = List<ClassData>();

  UserData.fromMap(Map<String, dynamic> data) {
    firstName = data['firstname'] ?? "";
    rating = data['rating'] ?? "";
    classes = data['classes'].cast<String>() ?? "";
  }

 

  List<ClassData> get cs => classList;

  set cs(List<ClassData> s) {
    cs = s;
  }

  Future get getTheUserClasses async {
    for (String c in classes) {
      DocumentSnapshot classsnapshot =
          await Firestore.instance.collection("Classes").document(c).get();

      final data =
          ClassData.fromUserMap(classsnapshot.data, classsnapshot.documentID);

      if (data != null) {
        classList.add(data);
        // print(data.classdescription);
      }
    }

    cs = classList;
  }

  UserData({this.firstName, this.rating, this.classes});
}

这是我加载此小部件时遇到的错误。

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building YourClasses(dirty, dependencies: [MediaQuery,
flutter: _InheritedProviderScope<UserDataNotifier>, _InheritedProviderScope<UserData>], state:
flutter: _YourClassesState#4bf6d):
flutter: The getter 'getTheUserClasses' was called on null.
flutter: Receiver: null
flutter: Tried calling: getTheUserClasses
flutter:
flutter: The relevant error-causing widget was:
flutter:   YourClasses
flutter:   file:///Users/devintripp/Desktop/flutter_apps.no_sync/discoverytutors/lib/Screens/LoggedIn/TutorsView/tutors.dart:148:65
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
flutter: #1      _YourClassesState.build (package:disc_t/Screens/LoggedIn/Classes/yourclasses.dart:66:38)
flutter: #2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
flutter: #3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
flutter: #4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
flutter: #5      Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
flutter: #6      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4481:5)
flutter: #7      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4666:11)
flutter: #8      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
flutter: ...     Normal element mounting (24 frames)
flutter: #32     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
flutter: #33     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5947:32)
flutter: ...     Normal element mounting (119 frames)
flutter: #152    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
flutter: #153    Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
flutter: #154    RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5580:32)
flutter: #155    MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5957:17)
flutter: #156    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
flutter: #157    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
flutter: #158    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
flutter: #159    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
flutter: #160    StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
flutter: #161    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
flutter: #162    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
flutter: #163    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
flutter: #164    ProxyElement.update (package:flutter/src/widgets/framework.dart:4862:5)
flutter: #165    _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:181:11)
flutter: #166    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
flutter: #167    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
flutter: #168    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
flutter: #169    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
flutter: #170    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
flutter: #171    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
flutter: #172    StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
flutter: #173    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
flutter: #174    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
flutter: #175    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
flutter: #176    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
flutter: #177    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
flutter: #178    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
flutter: #179    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
flutter: #180    StatelessElement.update (package:flutter/src/widgets/framework.dart:4583:5)
flutter: #181    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
flutter: #182    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
flutter: #183    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
flutter: #184    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
flutter: #185    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2627:33)
flutter: #186    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:883:20)
flutter: #187    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:284:5)
flutter: #188    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1113:15)
flutter: #189    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1052:9)
flutter: #190    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:968:5)
flutter: #194    _invoke (dart:ui/hooks.dart:261:10)
flutter: #195    _drawFrame (dart:ui/hooks.dart:219:3)
flutter: (elided 3 frames from dart:async)

这是首先加载的屏幕。

enter image description here

然后在复制时会获得3次相同的数据。这是在我刷新此小部件之后。

enter image description here

2 个答案:

答案 0 :(得分:1)

使用StreamBuilder代替使用FutureBuilder

而不是在每次构建时都创建Future,而是创建Future 一次并将其保存到您状态的变量中,这样就不会一次又一次地调用它,但无论调用构建方法多少次,都只有一次。

答案 1 :(得分:0)

Stream.fromFuture()不涵盖您的用例。如果添加新元素或删除元素,则不会更新(因为它只是从Future中获取元素)。

您可能需要执行以下操作:

Stream<List<YourModel>> getUserList() {
return Firestore.instance.collection('Classes')
    .snapshots()
    .map((snapShot) => snapShot.documents
    .map((document) => Yourmodel.fromDocument(document.data)
    .toList());
}

此外,为了避免在控制台中看到错误,您需要检查文档是否有数据(例如if (document.hasData())),如果尚无可用数据,则返回要显示的小部件。