1

I will show in 10 Text widgets, some variables. This variables can are Strings and can be empty ''

There is a simpler way to do this verification than this, one by one: ?

object.name.isNotEmpty ? object.name :  "not typed"

6 Answers 6

4

try this

check if it is null string

object.name ?? 'default value'

check if its a null or empty string

object.name?.isEmpty ?? 'default value'

The ?? double question mark operator means "if null". Take the following expression, for example. String a = b ?? 'hello'; This means a equals b, but if b is null then a equals 'hello'.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
type 'bool' is not a subtype of type 'String'
1
object.name ??= "not typed";

It assigns "not typed" if object.name is empty.

You can also use double question mark to check if something else is null and if it is assign the other value:

object.name = null ?? "not typed"; // result: object.name = "not typed";
object.name = "typed" ?? "not typed"; // result: object.name = "typed";

EDIT:

If you need to check if something is an empty string, you can use tenary expression, but there is no more operators or string methods:

object.name = object.name != null && object.name.isNotEmpty ? object.name : "not typed";

2 Comments

It doesn't work too. My variables are empty strings
So, not if String is null, but if string is empty. This is a huge difference
1

I think that the most convenient way to provide a default value is to extend the String class.

So, create a class StringExtension with a method like this:

extension StringExtension on String {
  String def(String defaultValue) {
    return isNotEmpty ? this : defaultValue;
  }
}

In your view, you can now simply do:

import 'package:code/helpers/string_extension.dart';
String value;

@override
Widget build(BuildContext context) {
  return Text(value.def("Unknown"))
}

Comments

0

If I understand your question correctly you want to simply check if something is null and assign to a variable accordingly. To do this you can use the OR operator like this:

var x = object.name || "not typed"

if object.name is truthy (as in not an empty string "") then that will be assigned to your variable (in this case x). If object.name is an empty string/falsey then "not-typed" will be assigned to x.

3 Comments

Thank you for the answer but it throws this error: The operands of the operator '||' must be assignable to 'bool'.
My variables are all strings and no one is null, some are "" empty
Unlike C, C++, Python, etc., Dart does not have permissive notions of "truthy" and "falsey". Only booleans are allowed in boolean expressions.
0

To check if a string is null or not by using ternary operator in JS you can use:

let n = ""

console.log(n.length > 0 ? "The string is not empty" : "The string is empty");

Comments

0

Try this:

object.name != Null ? object.name : "not typed"

or

object.name != '' ? object.name : "not typed"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.