file_magic_number 0.8.0 copy "file_magic_number: ^0.8.0" to clipboard
file_magic_number: ^0.8.0 copied to clipboard

A Flutter package to detect file types based on their magic number instead of MIME types. Supports Flutter on mobile, desktop, and web without native code.

example/lib/main.dart

import 'package:example/uint8list_extension.dart';
import 'package:file_magic_number/file_magic_number.dart';
import 'package:file_magic_number/magic_number_type.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'File Magic Number',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter File Magic Number Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  MagicNumberType? _typeFile;

  void _loadFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      withData: true,
    );
    if (result != null) {
      final bytes = result.files.single.bytes;
      bytes?.printInDebug(fileName: result.files.single.name);
      setState(() {
        _typeFile = MagicNumber.detectFileType(bytes);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('The loaded file is of type:'),
            Text(
              _typeFile?.name ?? "FILE NOT LOADED",
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _loadFile,
        tooltip: 'Load File',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
23
likes
160
points
1.21k
downloads

Publisher

verified publishervictorcarreras.dev

Weekly Downloads

A Flutter package to detect file types based on their magic number instead of MIME types. Supports Flutter on mobile, desktop, and web without native code.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, web

More

Packages that depend on file_magic_number