Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Variables and Null Safety in Flutter | Dart Language Core for Flutter
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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookVariables and Null Safety in Flutter

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1
some-alt