Is it possible to supply a default value to a Text widget? - flutter

As the question states, I would like to supply a default value to a Text Widget. I have four Text widgets. I use a variable in each Text Widget to display data. There is a chance that some data may not exist depending on the user and I would like to be able to default these Text Widgets to 0 if the value doesn't exist to prevent an error. Is this possible? Thanks!
Below is how it is currently.
Text(
'\$${quarter['data'][0].funds}',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold),
),

You can provide a default value using the ?? operator, which returns the expression on its left unless that expression’s value is null, in which case it evaluates and returns the expression on its right (and you might need to add question marks for conditional property access in case quarter['data'] and quarter['data'][0] are nullable):
Text(
'\$${quarter['data']?[0]?.funds ?? 0}',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
)
To safely access the funds at an index of the quarter['data'] list, you can create a helper method:
int fundsAt(int index) {
// This will return 0 if no element exists at the given index
return index >= 0 && index < quarter['data'].length
? quarter['data'][index].funds
: 0;
}
Here is the example of using fundsAt():
Text(
'\$${fundsAt(0)}',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
)

You can try something like this :
'\$${quarter['data'][0].funds ?? 0}'

You can try these:
If the value can be null, then you can try this:
Text(myNullableData ?? "Default value");
Text(myNullableData == null ? "" : "Default value");
The ?? operator use your value if it is not null. If yes, it will use the default value you want.
Otherwise if some data may not exist depending means that the variable can be just an empty string: '""', then you can use the ?: operator:
Text(myData == "" ? "Default value" : myData);

Related

This Code wont run due to "Expected Identifier" and "Expected to find ":" " in flutter

child: Text(
title,
style: TextStyle(color : Colors.grey[700], decoration: _enabledMap[title] == true ? TextDecoration.lineThrough)
),
Hi there, I am new to coding and I need help, please can you show me where I'm going wrong
You are providing data only for _enabledMap[title] == true. But you also need to provide when data is false for ternary case. You can provide null if you don't like to pass any TextDecoration
decoration: _enabledMap[title] == true
? TextDecoration.lineThrough
: null
For more info, see the Dart docs on conditional expressions

Printing int value in Text widget flutter

How do we print the int value in Text widget? I tried using '$num' but it just come out as instance of num.
This is what i tried so far
Text(
'$widget.count'+'. ',
style: TextStyle(
fontFamily: 'sansPro',
fontSize: 15,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
In your specific case you need to wrap the data with {}, So update the code to the following,
Text(
'${widget.count}''. ',
//...
);
And for more details:
To "print" or "render" a num value that is an int or a double in dart you can use the .toString() method on the variable. You can also use "String Interpolation" that is to evaluate a variable or an expression in a String.
For Example to create a Text widget that renders the a variable X:
Text(x.toString());
And using String Interpolation, you can use:
Text("$x");
If the variable needs to be evaluated as a part of an expression, That is if the vailable is part of an Object or you want to apply an operation before evaluating the final value, then the expression needs to be wrapped within {}, the code for that would be
Text("${x * 100}");
Text widget accept only data in a String format so you can get value in a int or double format and while displaying it convert it into String format. Consider a code snippet like a below:
intValueCount.toString()

Passing null to NumberFormat.simpleCurrency in flutter app

I am using NumberFormat.simpleCurrency to format dollar amounts in my flutter app. It works great if trxns.contractPrice is not null. When it is null I get the following error:
The getter 'isNegative' was called on null.
Receiver: null
Tried calling: isNegative
Here is the code snippet:
TextSpan(
text:
'\nPrice: ${NumberFormat.simpleCurrency().format(trxns.contractPrice) ?? 'n/a'}\nStatus: ${trxns.trxnStatus ?? 'n/a'}',
style: TextStyle(
fontWeight: FontWeight.w900,
color: Colors.blueGrey),
)
I have not been able to find anything for this error. Can anyone please help me with how to deal with nulls?
First check that traxns value is not null, then check its property or method contractPrice is not null. Right now, you're getting null for one of those things and an exception is being thown by the format method from NumberFormat. Once possiable example is:
TextSpan(
text:'\nPrice: ${trxns!.contractPrice == null ? 'n/a' : NumberFormat.simpleCurrency().format(trxns.contractPrice)}\nStatus: ${trxns!.trxnStatus ?? 'n/a'}',
style: TextStyle(fontWeight: FontWeight.w900, color: Colors.blueGrey),
);
Have a null check for the contractPrice before format
Sample:
Text(
'\nPrice: ${(contractPrice != null) ? NumberFormat.simpleCurrency().format(contractPrice) : 'n/a'}',
)

How to define TextStyle in a conditional statement flutter

Might be a very obvious question, but I need to add a TextStyle in a conditional statement, but it seems to not be applying it. This is what I tried:
Text((() {
if (contract.active == true) {
TextStyle(fontSize: 18);
return 'active';
}
return 'inactive';
}())),
It is not applying the textStyle. thank you.
You can use ternary operator:
Text(
'dummy text',
style: contract.active == true ? TextStyle(color: Colors.red): TextStyle(color:Colors.blue),
)
I added this for only TextStyle, you can do it for Text if you want.
And one thing, contract.active is already bool you don't need to check its equality, you can use contract.active instead of contract.active == true.
You can change text like this:
Text(
contract.active ? 'active' :'inactive'
style: contract.active ? TextStyle(fontSize: 18) : TextStyle()
)
You are using an immediate anonymous function there. The first parameter of Text widget expects a string and your anonymous function returns that. The TextStyle object that you've created inside the function body doesn't do anything.
You need to change your implementation to (using immediate anonymous function):
Text(
(() {
if (contract.active) {
return "Active";
}
return "Inactive";
}()),
style: (() {
if (contract.active) {
return TextStyle(fontSize: 18);
}
return null;
}())
),
But the above code looks very odd, you can simply achieve that using ternary operator:
Text(contract.active ? "Active" : "Inactive",
style: active ? TextStyle(fontSize: 18) : null),

Dropdown Menu based on List, A non-null String must be provided to a Text widget

I am trying to generate a list of dropdown options for my user. I have created a custom model to use and here it is:
class UserFontModel {
String fontFamily;
FontWeight fontWeight;
UserFontModel({this.fontFamily, this.fontWeight});
}
I then create an instance here:
List<UserFontModel> _fonts = [
UserFontModel(fontFamily: 'Bold', fontWeight: FontWeight.w700),
UserFontModel(fontFamily: 'Medium', fontWeight: FontWeight.w500),
UserFontModel(fontFamily: 'Regular', fontWeight: FontWeight.w400),
UserFontModel(fontFamily: 'Light', fontWeight: FontWeight.w300),
UserFontModel(fontFamily: 'Thin', fontWeight: FontWeight.w100),
];
I then create a dropdown menu with the items based on the above list:
new DropdownButton<String>(
hint: Text('Style'),
items: _fonts.map((fonts) => DropdownMenuItem<String> (
child: Container(
width: MediaQuery.of(context).size.width * 0.2,
child: Text(fonts.fontFamily, style: TextStyle(fontWeight: fonts.fontWeight),),
),
)).toList(),
onChanged: (String _) {
setState(() {
print(_);
});
},
),
But for some reason (probably a simple one that i'm missing), I can't work out why i'm getting the error:
A non-null String must be provided to a Text widget.
Any thoughts?
Well, my obvious thought is: post a minimal compilable example, with the full error message.
But another thought that might actually help you to find the mistake yourself is: don't make programming hard on yourself. You are human. Let the machine do the hard work, that is the way we should do this. You want to make sure you never accidentally have a null value in your models? Then write the model so a null value is not possible without your compiler telling you exactly where you failed at compile time:
class UserFontModel {
final String fontFamily;
final FontWeight fontWeight;
const UserFontModel({#required this.fontFamily, #required this.fontWeight})
: assert(fontFamily != null),
assert(fontWeight != null);
}
This class is not instatiable without both fontFamily and fontWeight given, it will warn you at runtime, if you gave one and it was null and it makes sure you cannot accidentally change the fields after they have been checked to be correct. The point here is: this is your compiler. the machine does the work for you. As it should be.
This will not fix your actual problem, but it should fix your problem of not finding your actual problem.