On this page17 sections
  1. Prerequisites
  2. Connection With Previous Lectures
  3. Learning Objectives
  4. 1. Why Bytes
  5. 2. Showing the Image
  6. 3. Hobbies With FilterChip
  7. 4. Copy the List When Editing
  8. 5. Save Everything
  9. 6. Show the Photo on the Dashboard
  10. Code Examples
  11. Common Beginner Mistakes
  12. Best Practices
  13. Mini Challenge
  14. Practice Questions
  15. Practice Answers
  16. Key Takeaways
  17. Full Code (Copy-Paste Reference)

Lecture 007: Image Storage & Hobby Chips

Category4: Images & Hobbies
Lecture007 of 007
Estimated video length25 min
TargetFlutter 3.44 · Dart 3.12 · hive 2.2
LevelBeginner
VideoPart 3: Image Storage, Hobbies with FilterChip & ValueListenableBuilder
Source codeGitHub: urraan_hive_final_code

The last two fields of our model: a profile photo stored as bytes, and hobbies chosen with FilterChips and stored as a List<String>. Then the dashboard shows the photo in a round avatar.

Contents

Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways · Full Code


Prerequisites

Lectures 001–006, and FilterChip, Wrap and DecorationImage from Essential Widgets.


Connection With Previous Lectures

The model already has profileImage (Uint8List?) and hobbies (List<String>). The form does not fill them yet. We add an image picker and chips, and the dashboard shows the result.


Learning Objectives

  • Pick an image with image_picker and read it as bytes
  • Store and show a Uint8List with MemoryImage
  • Build multi-select chips with FilterChip over a List<String>
  • Copy the list so editing does not change the saved record early
  • Show the avatar on the dashboard

1. Why Bytes

Hive cannot store a File path safely (the file may move or be cleared), but it can store Uint8List: the raw bytes. Picking returns an XFile; readAsBytes() gives the bytes.

dart
final imagePicker = ImagePicker();
Uint8List? _imageBytes;

Future<void> _pickImage(ImageSource source) async {
  final image = await imagePicker.pickImage(source: source);
  if (image == null) return;               // user cancelled
  final bytes = await image.readAsBytes();
  setState(() => _imageBytes = bytes);
}

ImageSource.gallery or ImageSource.camera. The null check handles a user who backs out.

2. Showing the Image

dart
Container(
  width: 200,
  height: 200,
  decoration: BoxDecoration(
    color: Colors.grey.shade200,
    borderRadius: BorderRadius.circular(15),
    image: _imageBytes != null
        ? DecorationImage(fit: BoxFit.cover, image: MemoryImage(_imageBytes!))
        : null,
  ),
  child: _imageBytes != null ? null : const Icon(Icons.image, size: 50),
)

MemoryImage turns bytes into an image provider. BoxFit.cover fills the box without stretching. When there is no image, an icon is the placeholder.

3. Hobbies With FilterChip

dart
final List<String> _hobbies = ['Cricket', 'Football', 'Hockey', 'Badminton'];
List<String> _selectedHobbies = [];
dart
Wrap(
  spacing: 8,
  children: [
    for (final hobby in _hobbies)
      FilterChip(
        label: Text(hobby),
        selected: _selectedHobbies.contains(hobby),
        onSelected: (selected) {
          setState(() {
            if (selected) {
              _selectedHobbies.add(hobby);
            } else {
              _selectedHobbies.remove(hobby);
            }
          });
        },
      ),
  ],
)

Wrap moves chips to the next line when the row is full. selected is true when the hobby is in the list; onSelected adds or removes it.

4. Copy the List When Editing

dart
_selectedHobbies = List.of(item.hobbies);

If you assign item.hobbies directly, tapping chips edits the saved record's list before you press Update. List.of makes a copy so changes only count when you save.

5. Save Everything

dart
final item = DataObjects()
  ..key = widget.item?.key ?? GeneralBox.getUniqueKey()
  ..name = controllerName.text.trim()
  ..address = controllerAddress.text.trim()
  ..profileImage = _imageBytes
  ..hobbies = _selectedHobbies;

6. Show the Photo on the Dashboard

In ListTile add a leading:

dart
leading: CircleAvatar(
  backgroundImage:
      item.profileImage != null ? MemoryImage(item.profileImage!) : null,
  child: item.profileImage == null ? const Icon(Icons.person) : null,
),

Code Examples

Example 1: The Finished Form

The finished lib/screen_add_update_record.dart is in the course repository at app/lib/screen_add_update_record.dart. It is Lecture 006's screen plus the fields, picker, preview, chips, and the copy in initState.

Example 2: Platform Setup for image_picker

  • iOS: add NSPhotoLibraryUsageDescription (and NSCameraUsageDescription for the camera) to ios/Runner/Info.plist.
  • Android: the gallery works without extra permissions on current Flutter; the camera uses the system camera app.
  • Web: works as a file chooser.

Common Beginner Mistakes

1. Not handling null from the picker: cancelling returns null; using it crashes.

2. Saving big photos: a full-resolution photo can be several MB per record, slowing the list. Reduce it: pickImage(source: ..., maxWidth: 600, imageQuality: 80).

3. Editing item.hobbies directly: copy with List.of (section 4).

4. Missing iOS usage description: the app closes the moment the picker opens.

5. context after await: if you ever call Navigator/ScaffoldMessenger after an await, check if (!mounted) return; first.


Best Practices

  • Resize images when picking (maxWidth, imageQuality).
  • Keep the list of hobbies in one constant list; later it can come from the box too.
  • Show a placeholder whenever the image is null.

Mini Challenge

Add a Remove photo button that sets _imageBytes = null, and a Camera button that uses ImageSource.camera.


Practice Questions

  1. Why bytes and not a file path? 2. What does readAsBytes return? 3. Why List.of in initState? 4. What stops the chips from overflowing the screen?

Practice Answers

  1. A path can break; bytes are stored inside the database itself.
  2. A Future<Uint8List>.
  3. So edits happen on a copy until the user saves.
  4. Wrap, which flows onto new lines.

Key Takeaways

  • Store images as Uint8List; show them with MemoryImage.
  • FilterChip + List<String> = multi-select; copy the list before editing.
  • You now have a complete Hive app: model, adapter, manager, live list, add/update, photo and hobbies.

Full Code (Copy-Paste Reference)

The complete, finished app: every file, as it stands at the end of the course.

lib/data_hive/data_objects.dart: the model

dart
import 'dart:typed_data';

import 'package:hive/hive.dart';

part 'data_objects.g.dart';

@HiveType(typeId: 0)
class DataObjects {
  @HiveField(0)
  dynamic key;

  @HiveField(1, defaultValue: '')
  String name = '';

  @HiveField(2, defaultValue: '')
  String address = '';

  @HiveField(3)
  Uint8List? profileImage; // raw photo bytes; null = no photo

  @HiveField(4, defaultValue: [])
  List<String> hobbies = [];
}

lib/data_hive/data_objects.g.dart: generated adapter (reference only)

dart
// GENERATED CODE - DO NOT MODIFY BY HAND.
// Produced by: dart run build_runner build --delete-conflicting-outputs

part of 'data_objects.dart';

class DataObjectsAdapter extends TypeAdapter<DataObjects> {
  @override
  final int typeId = 0;

  @override
  DataObjects read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return DataObjects()
      ..key = fields[0]
      ..name = fields[1] as String
      ..address = fields[2] as String
      ..profileImage = fields[3] as Uint8List?
      ..hobbies = (fields[4] as List).cast<String>();
  }

  @override
  void write(BinaryWriter writer, DataObjects obj) {
    writer
      ..writeByte(5)
      ..writeByte(0)
      ..write(obj.key)
      ..writeByte(1)
      ..write(obj.name)
      ..writeByte(2)
      ..write(obj.address)
      ..writeByte(3)
      ..write(obj.profileImage)
      ..writeByte(4)
      ..write(obj.hobbies);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is DataObjectsAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

lib/data_hive/general_box.dart: the manager

dart
import 'package:hive_flutter/hive_flutter.dart';

import 'data_objects.dart';

class GeneralBox {
  static const String dataObjects = 'DataObjects';

  static Future<void> init() async {
    await Hive.initFlutter();
    if (!Hive.isAdapterRegistered(DataObjectsAdapter().typeId)) {
      Hive.registerAdapter(DataObjectsAdapter());
    }
    if (!Hive.isBoxOpen(dataObjects)) {
      await Hive.openBox<DataObjects>(dataObjects);
    }
  }

  static Box<DataObjects> getBox() => Hive.box<DataObjects>(dataObjects);

  static String getUniqueKey() =>
      DateTime.now().millisecondsSinceEpoch.toString();

  static bool addUpdate(DataObjects item) {
    final box = getBox();
    box.put(item.key, item);
    return box.containsKey(item.key);
  }

  static bool delete(String key) {
    final box = getBox();
    box.delete(key);
    return !box.containsKey(key);
  }

  static List<DataObjects> getAll() => getBox().values.toList();
}

lib/screen_dashboard.dart: the live list with photo avatars

dart
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';

import 'data_hive/data_objects.dart';
import 'data_hive/general_box.dart';
import 'screen_add_update_record.dart';

class ScreenDashboard extends StatelessWidget {
  const ScreenDashboard({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Dashboard')),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (_) => const ScreenAddUpdateRecord()),
          );
        },
        child: const Icon(Icons.add),
      ),
      body: ValueListenableBuilder(
        valueListenable: GeneralBox.getBox().listenable(),
        builder: (context, Box<DataObjects> box, _) {
          if (box.values.isEmpty) {
            return const Center(child: Text('No data found'));
          }
          final values = box.values.toList();

          return ListView.builder(
            itemCount: values.length,
            itemBuilder: (context, index) {
              final item = values[index];
              return ListTile(
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (_) => ScreenAddUpdateRecord(item: item),
                    ),
                  );
                },
                // Shows the saved photo, or a person icon when there is none.
                leading: CircleAvatar(
                  backgroundImage: item.profileImage != null
                      ? MemoryImage(item.profileImage!)
                      : null,
                  child: item.profileImage == null
                      ? const Icon(Icons.person)
                      : null,
                ),
                title: Text(item.name),
                subtitle: Text(item.address),
                trailing: IconButton(
                  icon: const Icon(Icons.delete),
                  onPressed: () => GeneralBox.delete(item.key),
                ),
              );
            },
          );
        },
      ),
    );
  }
}

lib/screen_add_update_record.dart: form with image picker & hobby chips

dart
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

import 'data_hive/data_objects.dart';
import 'data_hive/general_box.dart';

class ScreenAddUpdateRecord extends StatefulWidget {
  final DataObjects? item;
  const ScreenAddUpdateRecord({super.key, this.item});

  @override
  State<ScreenAddUpdateRecord> createState() => _ScreenAddUpdateRecordState();
}

class _ScreenAddUpdateRecordState extends State<ScreenAddUpdateRecord> {
  final controllerName = TextEditingController();
  final controllerAddress = TextEditingController();
  final imagePicker = ImagePicker();

  // The fixed menu of hobbies the user can choose from.
  final List<String> _hobbies = ['Cricket', 'Football', 'Hockey', 'Badminton'];
  List<String> _selectedHobbies = [];

  Uint8List? _imageBytes;

  bool get _isUpdate => widget.item != null;

  @override
  void initState() {
    super.initState();
    final item = widget.item;
    if (item != null) {
      controllerName.text = item.name;
      controllerAddress.text = item.address;
      _imageBytes = item.profileImage;
      // Copy, don't alias: tapping chips must not edit the saved record
      // before the user presses Save.
      _selectedHobbies = List.of(item.hobbies);
    }
  }

  @override
  void dispose() {
    controllerName.dispose();
    controllerAddress.dispose();
    super.dispose();
  }

  Future<void> _pickImage(ImageSource source) async {
    // maxWidth/imageQuality keep the saved record small - Hive stores the full bytes.
    final image = await imagePicker.pickImage(
      source: source,
      maxWidth: 600,
      imageQuality: 80,
    );
    if (image == null) return; // user cancelled the picker
    final bytes = await image.readAsBytes();
    setState(() => _imageBytes = bytes);
  }

  void _save() {
    final item = DataObjects()
      ..key = widget.item?.key ?? GeneralBox.getUniqueKey()
      ..name = controllerName.text.trim()
      ..address = controllerAddress.text.trim()
      ..profileImage = _imageBytes
      ..hobbies = _selectedHobbies;

    if (GeneralBox.addUpdate(item)) {
      Navigator.pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(_isUpdate ? 'Update Record' : 'Add Record')),
      body: ListView(
        padding: const EdgeInsets.all(20),
        children: [
          Center(
            child: GestureDetector(
              onTap: () => _pickImage(ImageSource.gallery),
              child: Container(
                width: 200,
                height: 200,
                decoration: BoxDecoration(
                  color: Colors.grey.shade200,
                  borderRadius: BorderRadius.circular(15),
                  image: _imageBytes != null
                      ? DecorationImage(
                          fit: BoxFit.cover,
                          image: MemoryImage(_imageBytes!),
                        )
                      : null,
                ),
                child: _imageBytes != null
                    ? null
                    : const Icon(Icons.image, size: 50),
              ),
            ),
          ),
          const SizedBox(height: 20),
          TextField(
            controller: controllerName,
            decoration: const InputDecoration(
              labelText: 'Name',
              hintText: 'Enter name',
            ),
          ),
          const SizedBox(height: 10),
          TextField(
            controller: controllerAddress,
            decoration: const InputDecoration(
              labelText: 'Address',
              hintText: 'Enter address',
            ),
          ),
          const SizedBox(height: 20),
          // Wrap flows chips onto the next line instead of overflowing horizontally.
          Wrap(
            spacing: 8,
            children: [
              for (final hobby in _hobbies)
                FilterChip(
                  label: Text(hobby),
                  selected: _selectedHobbies.contains(hobby),
                  onSelected: (selected) {
                    setState(() {
                      if (selected) {
                        _selectedHobbies.add(hobby);
                      } else {
                        _selectedHobbies.remove(hobby);
                      }
                    });
                  },
                ),
            ],
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: _save,
            child: Text(_isUpdate ? 'Update Record' : 'Add Record'),
          ),
        ],
      ),
    );
  }
}

lib/main.dart

dart
import 'package:flutter/material.dart';

import 'data_hive/general_box.dart';
import 'screen_dashboard.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await GeneralBox.init();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(colorSchemeSeed: Colors.deepPurple),
      home: const ScreenDashboard(),
    );
  }
}

Platform setup required for image_picker: add NSPhotoLibraryUsageDescription (and NSCameraUsageDescription if you use the camera) to ios/Runner/Info.plist. Android and web work without extra config.

Video lecture

Lecture 007 · 25 min