Flutter - 必须向 Text 小部件提供非空字符串
Posted
技术标签:
【中文标题】Flutter - 必须向 Text 小部件提供非空字符串【英文标题】:Flutter - A non-null String must be provided to a Text widget 【发布时间】:2020-04-14 15:21:21 【问题描述】:关于使用 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'],
);
【问题讨论】:
接收到的数据好像是User类的对象实例,如果没有问题请提供它的代码。 @Sparks 我已对其进行了编辑以包含 User 类 您只有一个文本小部件,即 Text(author.fullname),因此很可能 author.fullname 为空。使用打印(作者。全名);在构建方法中验证它。试试 Text(author.fullname==null?'--':author.fullname) @Dev 正在执行热重启...在 1,248 毫秒内重新启动了应用程序。颤振:“用户”的实例颤振:null 颤振:“用户”的实例颤振:null 打印 (snapshot.data.fullname) 在控制台中为您提供什么? 【参考方案1】:如果名称为空,则必须提供空文本
return Column(
children: <Widget>[
Text(author.fullname == null? '' : author.fullname)
],
);
【讨论】:
颤动:空颤动:null。以上是关于Flutter - 必须向 Text 小部件提供非空字符串的主要内容,如果未能解决你的问题,请参考以下文章
必须向 Text 小部件 Flutter Api 提供非空字符串
data!= null :必须向 Text 小部件提供非 null 字符串