我正在使用此存储库中已完成的项目( https://github.com/londonappbrewery/destini-challenge-completed )
我在 Text() 小部件和后续调用中遇到错误。
storyBrain.getStory();
storyBrain.getChoice1();
storyBrain.getChoice2();
错误:
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'
解决方法 ——
我尝试使用
.toString()
将其转换为字符串但仍然得到同样的错误。flutter 医生 ——
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!
最佳答案
这是因为你的对象还没有初始化,为了解决这个问题,我们需要更多的代码,但为了避免空异常,使用 ??
像这样,它会检查它之前的内容是否为空,然后它会显示 ??
之后的值.
storyBrain.getStory() ?? 'default value';
storyBrain.getChoice1() ?? 'default value';
storyBrain.getChoice2() ?? 'default value';
关于flutter - 在 Text() 上出错 - 必须向 Text 小部件提供非空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62371215/