Flutter NoSuchMethodError引发了构建FutureBuilder <DocumentSnapshot>

时间:2019-10-27 10:23:55

标签: firebase flutter dart google-cloud-firestore flutter-dependencies

尝试从cloud_firestore提取用户数据时遇到此错误,但只有在用户首次登录应用并导航至个人资料屏幕时,才出现此错误。 如果我热重启或重新运行该应用,而在登录状态下错误消失

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══
flutter: The following NoSuchMethodError was thrown building StreamBuilder<DocumentSnapshot>(dirty, state:
flutter: _StreamBuilderBaseState<DocumentSnapshot, AsyncSnapshot<DocumentSnapshot>>#f33a1):
flutter: The method '[]' was called on null.
flutter: Receiver: null
flutter: Tried calling: []("name")
flutter:
flutter: User-created ancestor of the error-causing widget was:
flutter:   SliverFillRemaining
flutter:   file:///Users/ishangavidusha/Development/MUD/mud_mobile_app/lib/screens/profile_screen.dart:102:13
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
flutter: #1      new User.from (package:mud_mobile_app/models/user_model.dart:23:22)
flutter: #2      _ProfileScreenState.build.<anonymous closure> (package:mud_mobile_app/screens/profile_screen.dart:107:38)
flutter: #3      StreamBuilder.build (package:flutter/src/widgets/async.dart:425:74)
flutter: #4      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:125:48)
flutter: #5      StatefulElement.build (package:flutter/src/widgets/framework.dart:4047:27)
flutter: #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3941:15)
flutter: #7      Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5)
flutter: #8      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2348:33)
flutter: #9      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:760:20)
flutter: #10     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:280:5)
flutter: #11     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1033:15)
flutter: #12     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:975:9)
flutter: #13     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:891:5)
flutter: #17     _invoke (dart:ui/hooks.dart:249:10)
flutter: #18     _drawFrame (dart:ui/hooks.dart:207:3)
flutter: (elided 3 frames from package dart:async)

Emulator Screenshot

这是我使用的用户模型>

class User {
  final String id;
  final String name;
  final String profileImageUrl;
  final String email;

  User({this.id, this.name, this.profileImageUrl, this.email});

  factory User.fromDoc(DocumentSnapshot doc) {
    return User(
      id: doc.documentID,
      name: doc['name'],
      profileImageUrl: doc['profileImageUrl'],
      email: doc['email'],
    );
  }
}

FutureBuilder>

SliverFillRemaining(
  child: FutureBuilder(
    future: _getUserData(widget.userId),
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      if (!snapshot.hasData) {
        return Padding(
          padding: const EdgeInsets.all(50.0),
          child: Center(
            child: CircularProgressIndicator(),
          ),
        );
      }
      User user = User.fromDoc(snapshot.data);
      return Column(
        children: <Widget>[
          Padding(...),
          Container(...),
          Container(...),
        ],
      );
    }
  )
)

并且功能文档快照返回>

Future<DocumentSnapshot> _getUserData(userId) async {
  return Firestore.instance.collection('users').document(userId).get();
}

1 个答案:

答案 0 :(得分:0)

如果快照数据没有安全检查,通常会显示“StreamBuilder 脏状态”警告。这可确保快照包含数据。但是,似乎 < > 被设置为对您提供的代码段的安全检查。

从您提供的详细信息来看,该错误似乎仅在用户首次登录时发生,但在热重启或应用重启时错误消失。我建议检查方法 if (!snapshot.hasData) 是否能够在通常发生错误的第一次用户登录时接收 _getUserData(String userId)。根据您提供的日志,似乎在发生错误的地方传递了空值。

userId