【问题标题】:Getting error on Text() - A non-null String must be provided to a Text widgetText() 出错 - 必须向 Text 小部件提供非空字符串
【发布时间】:2020-06-14 10:23:11
【问题描述】:

我正在使用此存储库中已完成的项目(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() 将其转换为字符串,但仍然出现相同的错误。

颤振医生-

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!

【问题讨论】:

  • 检查您正在使用的文本小部件,您正在将空值传递给文本小部件。它将通过错误

标签: flutter dart


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    相关资源
    最近更新 更多