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:
- Utilizing conditional imports with
if (dart.library.html)to selectively importdart:iooruniversal_io. - Including express aliases (
dart_io.Fileanduniversal_io.File) to keep away from sort ambiguity. - Guaranteeing no
dart:iocode runs on net by guarding withkIsWeb.
// 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,
- Is there a approach to fully isolate the dart:io.File sort so it doesn’t battle with universal_io.File?
- 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_viewwhich might be appropriate withuniversal_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!
