首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取Text()上的错误-必须向文本小部件提供非空字符串。

获取Text()上的错误-必须向文本小部件提供非空字符串。
EN

Stack Overflow用户
提问于 2020-06-14 10:23:11
回答 1查看 91关注 0票数 2

我正在使用这个存储库(https://github.com/londonappbrewery/destini-challenge-completed)中完成的项目。

我在Text()小部件和后续调用中出错。

代码语言:javascript
复制
storyBrain.getStory();
storyBrain.getChoice1();
storyBrain.getChoice2();

错误:

代码语言:javascript
复制
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 285 pos 10: 'data != null'

WorkAround --我尝试使用.toString()将其转换为string,但仍然会出现相同的错误。

颤振博士-

代码语言:javascript
复制
E:\Temp\flutter\destini-challenge-starting>flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
> Flutter (Channel stable, v1.12.13+hotfix.9, on Microsoft Windows [Version 10.0.18363.778], locale en-IN)

> Android toolchain - develop for Android devices (Android SDK version 29.0.2)
> Android Studio (version 3.6)
> VS Code, 64-bit edition (version 1.44.2)
> Connected device (1 available)

• No issues found!
EN

回答 1

Stack Overflow用户

发布于 2020-06-14 11:16:35

这是因为您的对象还没有初始化,为了解决这个问题,我们需要更多的代码,但是为了避免出现空异常,请像这样使用??,它将检查它之前的值是否为空,然后它将在??之后显示值。

代码语言:javascript
复制
storyBrain.getStory() ?? 'default value';
storyBrain.getChoice1() ?? 'default value';
storyBrain.getChoice2() ?? 'default value';
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62371215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档