在尝试使用FutureBuilder从Firestore提取数据时遇到错误。如果用户登录并打印(snapshot.data),则会从Firestore中提取正确的快照:
flutter: FirebaseUser({displayName: null, providerId: Firebase, uid: UFubdB2xPCTUu8ESgQikOr9WOe52, phoneNumber: null, isEmailVerified: false, providerData: [{email: titi@momo.com, providerId: password, photoUrl: null, displayName: null, uid: titi@momo.com, phoneNumber: null}], email: titi@momo.com, photoUrl: null, lastSignInTimestamp: 1576367769805, isAnonymous: false, creationTimestamp: 1576367424402})
但是,如果我导航到“提要”屏幕,则在我打印(snapshot.data ['email'])时会抛出异常
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state:
flutter: _FutureBuilderState<DocumentSnapshot>#5d856):
flutter: The method '[]' was called on null.
flutter: Receiver: null
flutter: Tried calling: []("email")
flutter:
flutter: The relevant error-causing widget was:
flutter: FutureBuilder<DocumentSnapshot> file:///Users/momo/Desktop/combine/lib/screens/feed.dart:23:13
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 _FeedState.build.<anonymous closure> (package:combine/screens/feed.dart:26:30)
flutter: #2 _FutureBuilderState.build (package:flutter/src/widgets/async.dart)
flutter: #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
flutter: #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
flutter: #5 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #7 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #8 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #10 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #11 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #12 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #13 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #14 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #15 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #16 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #18 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #19 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #20 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #21 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #22 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #24 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #25 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #26 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #27 ParentDataElement.mount (package:flutter/src/widgets/framework.dart:4617:11)
flutter: #28 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #29 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5551:32)
flutter: #30 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #31 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #32 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #33 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #34 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #35 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #36 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #37 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #38 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #39 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #40 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #41 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #42 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #43 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #44 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #45 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #46 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #47 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #48 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #49 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #50 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #51 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #52 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #53 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #54 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #55 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #56 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #57 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #58 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #59 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #60 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #61 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #62 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #63 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #64 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #65 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #66 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #67 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #68 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #69 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #70 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #71 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #72 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #73 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #74 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #75 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #76 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #77 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #78 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #79 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #80 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #81 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #82 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #83 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #84 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #85 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #86 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #87 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #88 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #89 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #90 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #91 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #92 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #93 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #94 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #95 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #96 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #97 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #98 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #99 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #100 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #101 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #102 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #103 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #104 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #105 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #106 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #107 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #108 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #109 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #110 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #111 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #112 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #113 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #114 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #115 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #116 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #117 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #118 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #119 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #120 ParentDataElement.mount (package:flutter/src/widgets/framework.dart:4617:11)
flutter: #121 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #122 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #123 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #124 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #125 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #126 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #127 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #128 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #129 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #130 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #131 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #132 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #133 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #134 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #135 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #136 SliverMultiBoxAdaptorElement.updateChild (package:flutter/src/widgets/sliver.dart:1288:36)
flutter: #137 SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1273:20)
flutter: #138 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2412:19)
flutter: #139 SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1266:11)
flutter: #140 RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:354:23)
flutter: #141 RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1823:58)
flutter: #142 PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:875:15)
flutter: #143 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1823:13)
flutter: #144 RenderSliverMultiBoxAdaptor._createOrObtainChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:343:5)
flutter: #145 RenderSliverMultiBoxAdaptor.insertAndLayoutChild (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:489:5)
flutter: #146 RenderSliverFixedExtentBoxAdaptor.performLayout (package:flutter/src/rendering/sliver_fixed_extent_list.dart:255:17)
flutter: #147 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7)
flutter: #148 RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:134:11)
flutter: #149 _RenderSliverFractionalPadding.performLayout (package:flutter/src/widgets/sliver.dart:1165:11)
flutter: #150 RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7)
flutter: #151 RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:410:13)
flutter: #152 RenderViewport._attemptLayout (package:flutter/src/rendering/viewport.dart:1367:12)
flutter: #153 RenderViewport.performLayout (package:flutter/src/rendering/viewport.dart:1285:20)
flutter: #154 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1584:7)
flutter: #155 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:844:18)
flutter: #156 RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:344:19)
flutter: #157 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:774:13)
flutter: #158 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
flutter: #159 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
flutter: #160 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9)
flutter: #161 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:957:5)
flutter: #165 _invoke (dart:ui/hooks.dart:259:10)
flutter: #166 _drawFrame (dart:ui/hooks.dart:217:3)
flutter: (elided 3 frames from package dart:async)
flutter:
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: NoSuchMethodError: The method '[]' was called on null.
flutter: titi@momo.com
但是,如果我在“源”屏幕上仅打印(snapshot.data),则在获取实例之前会抛出null。
flutter: null
flutter: null
flutter: Instance of 'DocumentSnapshot'
从主页导航
class MyApp extends StatelessWidget {
Widget _getScreenId() {
return StreamBuilder<FirebaseUser>(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
print(snapshot.data);
return Landing(userId: snapshot.data.uid);
} else {
return Login();
}
});
}
从着陆导航到Feed屏幕
children: <Widget>[
Home(),
Feed(userId: widget.userId),
Profile(),
],
Feed屏幕
class Feed extends StatefulWidget {
final String userId;
Feed({this.userId});
@override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 1,
title: Text('feed screen'),
),
backgroundColor: Colors.white,
body: FutureBuilder(
future: usersRef.document(widget.userId).get(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.data['email']);
return Padding(
padding: const EdgeInsets.only(top: 70, left: 20, right: 20),
child: Column(
children: <Widget>[
Text('MeandYou')
],
),
);
},
),
);
}
}
用户参考
final usersRef = _firestore.collection('users');
用户模型
User(
{this.id,
this.email,
this.password,
this.fullname,
this.username,
this.mobile,
this.profileImgUrl,
this.bio});
factory User.fromDoc(DocumentSnapshot doc) {
return User(
id: doc.documentID,
email: doc['email'],
fullname: doc['fullname'],
username: doc['username'],
mobile: doc['mobile'],
profileImgUrl: doc['profileImgUrl'],
bio: doc['bio'] ?? ''
);
}
我尝试了几种选择,希望能有所帮助
答案 0 :(得分:0)
未来并不总是准备就绪,它们是异步的。我会至少插入 空检查,但这是一种更好的方法。最好检查ConnectionState
中的Future
,以检查其是否准备就绪,然后检查数据是否为空。使用ConnectionState
的另一个好处是可以检查它是否在等待某些东西。然后,您可以显示加载图标或其他内容。
class _FeedState extends State<Feed> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 1,
title: Text('feed screen'),
),
backgroundColor: Colors.white,
body: FutureBuilder(
future: usersRef.document(widget.userId).get(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print(snapshot.data['email']);
return Padding(
padding: const EdgeInsets.only(top: 70, left: 20, right: 20),
child: Column(
children: <Widget>[
Text('MeandYou')
],
),
);
}
}
},
),
);
}
}