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"
0nroth1
  • 201
  • 2
  • 11

6 Answers6

2

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'.

Mr Random
  • 1,992
  • 7
  • 20
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) 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. – wscourge Nov 27 '20 at 07:26
  • `type 'bool' is not a subtype of type 'String'` – 0nroth1 Nov 27 '20 at 10:59
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";
Owczar
  • 2,335
  • 1
  • 17
  • 24
  • It doesn't work too. My variables are empty strings – 0nroth1 Nov 26 '20 at 20:52
  • So, not if String is null, but if string is empty. This is a [huge difference](https://stackoverflow.com/questions/16620354/difference-between-null-and-empty-string) – Owczar Nov 26 '20 at 20:55
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"))
}
LawCha
  • 113
  • 1
  • 10
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.

Faris Aziz
  • 144
  • 4
  • Thank you for the answer but it throws this `error: The operands of the operator '||' must be assignable to 'bool'.` – 0nroth1 Nov 26 '20 at 20:45
  • My variables are all strings and no one is null, some are "" empty – 0nroth1 Nov 26 '20 at 20:47
  • Unlike C, C++, Python, etc., Dart does not have permissive notions of "truthy" and "falsey". Only booleans are allowed in boolean expressions. – jamesdlin Nov 26 '20 at 23:34
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");
0

Try this:

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

or

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

Scoop561
  • 73
  • 1
  • 5