Manjot Singh Plaha
Manjot Singh Plaha

Reputation: 21

Getting error on Text() - A non-null String must be provided to a Text widget

I am using the completed project from this repository(https://github.com/londonappbrewery/destini-challenge-completed)

I am getting error on Text() widget and in follow calls.

storyBrain.getStory();
storyBrain.getChoice1();
storyBrain.getChoice2();

error:

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 - I tried converting it to string using .toString() but still getting same error.

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!

Upvotes: 2

Views: 110

Answers (1)

hewa jalal
hewa jalal

Reputation: 961

it's because your object didn't got initialized yet, and to solve that we need more code but to save yourself from null exception use ?? like this it will check if what's before it is null then it will display the value after ??.

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

Upvotes: 1

Related Questions