In FLUTTER / DART, why do we sometimes add a question mark to "String" when declaring a variable?

3,823

Solution 1

variable_type ? name_of_variable; means that name_of_variable can be null.

variable_type name_of_variable1; means that name_of_variable1 cannot be null and you should initialize it immediately.

late variable_type name_of_variable2; means that name_of_variable2 cannot be null and you can initialize it later.

late variable_type name_of_variable3;
variable_type ? name_of_variable4;
name_of_variable4=some_data;
name_of_variable3=name_of_variable4!;// name_of_variable4! means that you are sure 100% it never will be null

Real example with int type:

int ? a=null; // OK
int  b=null; // b cannot be equal null
late int c =null; // c cannot be equal null
late int d;
d=5; //OK

late int d; // if you never initialize it, you ll got exception

int e=5;
int? f=4;
int ? z;
e=f!; // OK, You are sure that f never are null

e=z!;// You get exception because z is equal null

Solution 2

This is with null safety, the question mark means that this String? can possibly be null and flutter will allow you to assign null to it. String can never be null and you'll get an error before compiling.

If you define a variable String? name, and you want to use it later in a Text widget, you'll get an error. Because Text widgets only accept non-nullable types. But if you are sure that name will never be null, you tell flutter to not worry about it and that you know what you are doing, you do this by adding the ! like this : Text(name!).

Share:
3,823
SylvainJack
Author by

SylvainJack

I have worked as a teacher for about 25 years and decided I wanted to embrace a new career. I started learning Flutter / Dart in January 2020. I know it will be a long way to be able to develop apps on my own, but I want to prove that I can do it!

Updated on December 28, 2022

Comments

  • SylvainJack
    SylvainJack over 1 year

    In the demo app, we find an instance of "final String? title;" - > Why do we add this "?" after the type String ?

    class MyHomePage extends StatefulWidget {
      MyHomePage({Key? key, this.title}) : super(key: key);
    
      **final String? title;**
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    

    In the same way, why when using it, we add a "!" ?

    return Scaffold(
          appBar: AppBar(
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: **Text(widget.title!),**
          ),
          body: Center(
    
  • SylvainJack
    SylvainJack about 3 years
    I was wondering if it had something to do with null safety. Thank you so much for your quick answer. What about the "!" in "TEXT(WIDGET.TITLE!) ?
  • Huthaifa Muayyad
    Huthaifa Muayyad about 3 years
    I edited my answer, and believe that I answered your second question regarding the !.
  • Huthaifa Muayyad
    Huthaifa Muayyad about 3 years
    It has everything to do with null safety, it is writing dart in null-safety. You should check it out and read into, it'll make your code faster, smaller and less prone to bugs.
  • SylvainJack
    SylvainJack about 3 years
    Thank you very much for your answer. Very clear! As you might have guessed, I'm a beginner with flutter development. But loving it ! :)
  • SylvainJack
    SylvainJack about 3 years
    Was wondering if "virtual teams" of developers can be created in Stack Overflow, so that beginners like me can share with more "advanced" developers ?
  • SylvainJack
    SylvainJack about 3 years
    Thank you for your explanation ! It helps a lot.
  • SylvainJack
    SylvainJack about 3 years
    What does "late" mean in your examples ?
  • Huthaifa Muayyad
    Huthaifa Muayyad about 3 years
    Keep learning and investing in yourself, ask well written questions and you'll get better answers. You get better with time with and practice, everybody is learning everyday. You're most welcome!
  • SylvainJack
    SylvainJack about 3 years