在Text()上获取错误-必须将非null字符串提供给Text小部件

时间:2020-06-14 10:23:11

标签: flutter dart

我正在使用此存储库中的已完成项目(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'

enter image description here

WorkAround - 我尝试使用.toString()将其转换为字符串,但仍然遇到相同的错误。

Flutter Doctor -

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!

1 个答案:

答案 0 :(得分:1)

这是因为您的对象尚未初始化,要解决我们需要更多代码,但为了避免空异常,请使用??这样,它将检查是否为空,然后将其显示??之后的值。

storyBrain.getStory() ?? 'default value';
storyBrain.getChoice1() ?? 'default value';
storyBrain.getChoice2() ?? 'default value';