关于使用futureBuilder从Firestore中获取数据时出现错误。当我在不调用Text小部件的情况下打印snapshot.data时,得到了“用户”的实例。
不致电author.fullname
body: ListView.builder(
itemCount: _posts.length,
itemBuilder: (BuildContext context, int index) {
Post post = _posts[index];
return FutureBuilder(
future: DatabaseService.getUserWithId(post.authorid),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print(snapshot.data);
User author = snapshot.data;
return Column(
children: <Widget>[],
);
}
}
return SizedBox.shrink();
输出
Performing hot restart...
Restarted application in 1,146ms.
flutter: Instance of 'User'
flutter: Instance of 'User'
但是,当我尝试从Firestore要求用户的全名时,它返回null
body: ListView.builder(
itemCount: _posts.length,
itemBuilder: (BuildContext context, int index) {
Post post = _posts[index];
return FutureBuilder(
future: DatabaseService.getUserWithId(post.authorid),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print(snapshot.data);
User author = snapshot.data;
return Column(
children: <Widget>[
Text(author.fullname)
],
);
}
}
return SizedBox.shrink();
输出
Performing hot restart...
Restarted application in 1,042ms.
flutter: Instance of 'User'
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
flutter: The following assertion was thrown building FutureBuilder<User>(dirty, state:
flutter: _FutureBuilderState<User>#23d74):
flutter: A non-null String must be provided to a Text widget.
flutter: 'package:flutter/src/widgets/text.dart':
flutter: Failed assertion: line 285 pos 10: 'data != null'
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter: https://github.com/flutter/flutter/issues/new?template=BUG.md
flutter:
flutter: The relevant error-causing widget was:
flutter: FutureBuilder<User> file:///Users/momo/Desktop/combine/lib/screens/home.dart:65:18
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #2 new Text (package:flutter/src/widgets/text.dart:285:10)
flutter: #3_HomeState.build.<anonymous closure>.<anonymous closure> (package:combine/screens/home.dart:75:23)
flutter: #4 _FutureBuilderState.build (package:flutter/src/widgets/async.dart)
flutter: #5 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
flutter: #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
flutter: #7 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #8 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2432:33)
flutter: #9 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:773:20)
flutter: #10 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
flutter: #11 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
flutter: #12 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9)
flutter: #13 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:957:5)
flutter: #17 _invoke (dart:ui/hooks.dart:259:10)
flutter: #18 _drawFrame (dart:ui/hooks.dart:217:3)
flutter: (elided 5 frames from class _AssertionError and package dart:async)
用户类别
class User {
final String id;
final String email;
final String password;
final String fullname;
User(
{this.id,
this.email,
this.password,
this.fullname,});
factory User.fromDoc(DocumentSnapshot doc) {
return User(
id: doc.documentID,
email: doc['email'],
fullname: doc['fullname'],
);
}
}
答案 0 :(得分:0)
如果名称为空,则必须提供一个空文本
return Column(
children: <Widget>[
Text(author.fullname == null? '' : author.fullname)
],
);