Flutter State Restoration: Uygulama Kapatıldığında UI Durumunu Geri Yüklemek

Flutter'da kullanıcı uygulamayı kapatıp tekrar açtığında, önceki ekran durumunu (scrolling konumu, form verileri, sekmeler) geri yüklemek için State Restoration mekanizması kullanılır. Bu, Android'de "Process Death" veya iOS'ta "app backgrounding" senaryolarında hayat kurtarır.

State Restoration'ı aktifleştirmek için üç adım gerekir: `RestorationId` tanımla, `Restorable` property'ler kullan ve `RestorationBucket` yapılandır.

1import 'package:flutter/material.dart';
2
3void main() {
4runApp(const MyApp());
5}
6
7class MyApp extends StatelessWidget {
8const MyApp({Key? key}): super(key: key);
9
10@override
11Widget build(BuildContext context) {
12return MaterialApp(
13title: 'State Restoration Demo',
14restorationScopeId: 'app', // Restoration etkinleştir
15home: const MyHomePage(),
16);
17}
18}
19
20class MyHomePage extends StatefulWidget {
21const MyHomePage({Key? key}): super(key: key);
22
23@override
24State<MyHomePage> createState() => _MyHomePageState();
25}
26
27class _MyHomePageState extends State<MyHomePage> with RestorationMixin {
28late RestorableInt _counter;
29late RestorableStringValue _inputText;
30
31@override
32String get restorationId => 'home_page'; // State ID'si
33
34@override
35void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
36registerForRestoration(_counter, 'counter');
37registerForRestoration(_inputText, 'input_text');
38}
39
40@override
41void initState() {
42super.initState();
43_counter = RestorableInt(0);
44_inputText = RestorableStringValue('');
45}
46
47@override
48void dispose() {
49_counter.dispose();
50_inputText.dispose();
51super.dispose();
52}
53
54void _incrementCounter() {
55setState(() {
56_counter.value++;
57});
58}
59
60@override
61Widget build(BuildContext context) {
62return Scaffold(
63appBar: AppBar(title: const Text('State Restoration')),
64body: Center(
65child: Column(
66mainAxisAlignment: MainAxisAlignment.center,
67children: [
68Text('Counter: ${_counter.value}',
69style: Theme.of(context).textTheme.headlineMedium),
70const SizedBox(height: 20),
71TextField(
72onChanged: (value) => _inputText.value = value,
73decoration: InputDecoration(
74hintText: 'Metin gir (kaydedilecek)',
75border: OutlineInputBorder(),
76),
77),
78const SizedBox(height: 20),
79Text('Girilen: ${_inputText.value}'),
80],
81),
82),
83floatingActionButton: FloatingActionButton(
84onPressed: _incrementCounter,
85child: const Icon(Icons.add),
86),
87);
88}
89}

Beklenen Çıktı: Uygulamayı açıp counter'ı 5'e çıkar, metin gir, uygulamayı kapatıp tekrar aç sayaç ve metin aynen geri yüklenir.

Reklam · TEST
AdSense Test ReklamıYazı arası (gönderi içi) · Yazı arası

Sık Yapılan Hatalar:

1. `restorationScopeId` unutmak, `MaterialApp` seviyesinde bu olmadan restoration çalışmaz.
2. `RestorableBool`, `RestorableInt` gibi özel türler kullanmamak, normal `int` veya `String` kaydedilmez, `Restorable` wrapper gerekir.
3. `restoreState()` içinde register etmeyi unutmak, property'ler `registerForRestoration()` ile kayıtlı olmalı.
4. Dispose etmemek, memory leak'e neden olur.

Android Process Death veya iOS background kill senaryolarında bu mekanizma otomatik devreye girer. Eğer özel veri (API response, complex object) kaydetmek istersen `RestorableProperty` extend ederek custom sınıf yazabilirsin.

Reklam · TEST
AdSense Test ReklamıGönderi altı · Görüntülü
konu hakkında tepkinizi verebilirsiniz