6.4 C
Canberra
Monday, October 27, 2025

Flutter app improvement: epub_view, dart:io, and universal_io incompatibility. Easy methods to resolve?


I am attempting to construct a cross-platform e-reader app in Flutter. To make sure compatibility with net, I’ve been utilizinguniversal_io. BUT the epub_view bundle apparently doesn’t assist universal_io, and solely dart:io completely… and for a e-reader app, epub assist is crucial.

To deal with this, I attempted to conditionally use dart:io.File for non-web platforms whereas excluding it on net.

The error I can not do away with:

flutter run -d chrome
Launching libmain.dart on Chrome in debug mode...
lib/src/options/book_reader/elements/epub_viewer.dart:58:41: Error: The argument sort 'File/*1*/' cannot be assigned to the parameter sort 'File/*2*/'.
 - 'File/*1*/' is from 'dart:io'.
 - 'File/*2*/' is from 'bundle:universal_file/src/io/file.dart' ('/C:/Customers/Larry/AppData/Native/Pub/Cache/hosted/pub.dev/universal_file-1.0.0/lib/src/io/file.dart').
        doc: EpubDocument.openFile(file),
                                        ^

Right here’s the newest model of my code, together with my makes an attempt to resolve this concern:

  1. Utilizing conditional imports with if (dart.library.html) to selectively import dart:io or universal_io.
  2. Including express aliases (dart_io.File and universal_io.File) to keep away from sort ambiguity.
  3. Guaranteeing no dart:io code runs on net by guarding with kIsWeb.
// src/options/book_reader/elements/epub_viewer.dart
import 'bundle:flutter/materials.dart';
import 'bundle:flutter/basis.dart' present kIsWeb;
import 'bundle:epub_view/epub_view.dart';

// Explicitly import dart:io with an alias
import 'dart:io' as dart_io;

// Explicitly import universal_io with an alias
import 'bundle:universal_io/io.dart' as universal_io;

class EpubViewerScreen extends StatefulWidget {
  last String filePath;
  last String? title;
  last Perform(double)? onProgressChanged;
  last double initialProgress;

  const EpubViewerScreen({
    Key? key,
    required this.filePath,
    this.title,
    this.onProgressChanged,
    this.initialProgress = 0.0,
  }) : tremendous(key: key);

  @override
  State createState() => _EpubViewerScreenState();
}

class _EpubViewerScreenState extends State {
  late EpubController _epubController;
  bool _isLoading = true;
  String? _errorMessage;

  @override
  void initState() {
    tremendous.initState();
    _loadBook();
  }

  Future _loadBook() async {
    attempt {
      if (kIsWeb) {
        // EPUB not supported on the internet
        throw UnsupportedError('EPUB viewing will not be supported on the internet.');
      }

      // Use dart:io.File for non-web platforms
      last dart_io.File file = dart_io.File(widget.filePath);

      // Make sure the file exists
      if (!await file.exists()) {
        throw dart_io.FileSystemException('File not discovered', widget.filePath);
      }

      // Initialize the EpubController with the dart:io.File
      _epubController = EpubController(
        doc: EpubDocument.openFile(file),
      );

      setState(() {
        _isLoading = false;
        _errorMessage = null;
      });
    } catch (e) {
      setState(() {
        _errorMessage="Error loading EPUB: ${e.toString()}";
        _isLoading = false;
      });
    }
  }

  @override
  Widget construct(BuildContext context) {
    if (kIsWeb) {
      return Scaffold(
        appBar: AppBar(title: Textual content(widget.title ?? 'EPUB Reader')),
        physique: const Middle(
          youngster: Textual content('EPUB viewing will not be supported on the internet platform.'),
        ),
      );
    }

    if (_isLoading) {
      return Scaffold(
        appBar: AppBar(title: Textual content(widget.title ?? 'EPUB Reader')),
        physique: const Middle(youngster: CircularProgressIndicator()),
      );
    }

    if (_errorMessage != null) {
      return Scaffold(
        appBar: AppBar(title: Textual content(widget.title ?? 'EPUB Reader')),
        physique: Middle(
          youngster: Padding(
            padding: const EdgeInsets.all(16.0),
            youngster: Column(
              mainAxisAlignment: MainAxisAlignment.middle,
              youngsters: [
                const Icon(Icons.error_outline, color: Colors.red, size: 48),
                const SizedBox(height: 16),
                Text(
                  _errorMessage!,
                  textAlign: TextAlign.center,
                  style: const TextStyle(fontSize: 16),
                ),
                const SizedBox(height: 24),
                ElevatedButton(
                  onPressed: _loadBook,
                  child: const Text('Retry'),
                ),
              ],
            ),
          ),
        ),
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: Textual content(widget.title ?? 'EPUB Reader'),
      ),
      physique: EpubView(
        controller: _epubController,
        onChapterChanged: (chapter) {
          if (widget.onProgressChanged != null && chapter != null) {
            widget.onProgressChanged!(0.5);
          }
        },
      ),
    );
  }

  @override
  void dispose() {
    _epubController.dispose();
    tremendous.dispose();
  }
}

So,

  1. Is there a approach to fully isolate the dart:io.File sort so it doesn’t battle with universal_io.File?
  2. Are there higher methods to strategy cross-platform compatibility for file dealing with on this state of affairs? For instance, are there any cross-platform alternate options to epub_view which might be appropriate with universal_io?

I’d drastically respect any steering. I’ve been caught on this concern for days and am beginning to really feel like I’m moving into circles.

Thanks prematurely on your assist!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles