Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Variables and Null Safety in Flutter | Dart Language Core for Flutter
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Dart for Flutter Developers

bookVariables and Null Safety in Flutter

Note
Definition

Null-aware operators in Dart, such as ??, ?., and ??=, help you write safer code by handling cases where a variable might be null. In Flutter, these operators are commonly used in widget build methods to provide default values or avoid calling methods on null objects, reducing the risk of runtime exceptions and ensuring smoother UI updates.

Understanding how to declare and use variables is essential when building Flutter apps with Dart. Dart provides three main ways to declare variables: var, final, and const. Use var when a variable's value might change, and Dart will infer its type. Use final for single-assignment variables whose value is set once and never changes at runtime. Use const for compile-time constants—values that are known and fixed at compile time. Dart's type system also supports null safety, which means you must explicitly allow a variable to be null by adding a question mark (?) to its type. By default, variables are non-nullable, which helps prevent many common runtime errors.

main.dart

main.dart

copy
12345678910111213141516171819202122232425
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // Non-nullable variable final String title = "Hello Flutter"; // Nullable variable final String? subtitle = null; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text(title)), body: Center( child: Text(subtitle ?? "No subtitle provided"), ), ), ); } }

Null safety in Dart helps you avoid runtime errors by ensuring that non-nullable variables cannot accidentally contain null values. This feature is especially important in Flutter, where widget trees rely on predictable, non-null data to render correctly. By making nullability explicit, Dart helps you catch potential issues at compile time rather than during app execution.

main.dart

main.dart

copy
123456789101112131415161718192021
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { final String? greeting = null; @override Widget build(BuildContext context) { // Use the null-aware operator ?? to provide a fallback value return MaterialApp( home: Scaffold( body: Center( child: Text(greeting ?? "Welcome!"), ), ), ); } }
question mark

Which statement about Dart variables and null safety is correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you give examples of how to declare variables using var, final, and const in Dart?

How do I make a variable nullable in Dart?

Why is null safety important in Flutter development?

bookVariables and Null Safety in Flutter

Swipe um das Menü anzuzeigen

Note
Definition

Null-aware operators in Dart, such as ??, ?., and ??=, help you write safer code by handling cases where a variable might be null. In Flutter, these operators are commonly used in widget build methods to provide default values or avoid calling methods on null objects, reducing the risk of runtime exceptions and ensuring smoother UI updates.

Understanding how to declare and use variables is essential when building Flutter apps with Dart. Dart provides three main ways to declare variables: var, final, and const. Use var when a variable's value might change, and Dart will infer its type. Use final for single-assignment variables whose value is set once and never changes at runtime. Use const for compile-time constants—values that are known and fixed at compile time. Dart's type system also supports null safety, which means you must explicitly allow a variable to be null by adding a question mark (?) to its type. By default, variables are non-nullable, which helps prevent many common runtime errors.

main.dart

main.dart

copy
12345678910111213141516171819202122232425
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // Non-nullable variable final String title = "Hello Flutter"; // Nullable variable final String? subtitle = null; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text(title)), body: Center( child: Text(subtitle ?? "No subtitle provided"), ), ), ); } }

Null safety in Dart helps you avoid runtime errors by ensuring that non-nullable variables cannot accidentally contain null values. This feature is especially important in Flutter, where widget trees rely on predictable, non-null data to render correctly. By making nullability explicit, Dart helps you catch potential issues at compile time rather than during app execution.

main.dart

main.dart

copy
123456789101112131415161718192021
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { final String? greeting = null; @override Widget build(BuildContext context) { // Use the null-aware operator ?? to provide a fallback value return MaterialApp( home: Scaffold( body: Center( child: Text(greeting ?? "Welcome!"), ), ), ); } }
question mark

Which statement about Dart variables and null safety is correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1
some-alt