Last active
October 13, 2024 19:31
-
-
Save itskgore/ab42d8aca86e3ca680ed3e6a19d15986 to your computer and use it in GitHub Desktop.
Solving Cross-Platform Import Issues in Flutter (Web & Mobile) ππ± As Flutter developers, managing platform-specific imports for mobile and web often leads to separate branches and frustrating compile-time errors. To solve this, Iβve created a simple, efficient solution using an abstract class that differentiates platform-specific imports withouβ¦
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:typed_data'; | |
import 'dart:html' as html; | |
import 'dart:js' as js; | |
CustomPlatform getInstance() => CommonImport(); | |
class CommonImport implements CustomPlatform { | |
@override | |
void removeLocalStorage() { | |
html.window.localStorage.remove('okta-token-storage'); | |
} | |
@override | |
void copyHtmlFromWeb(String content) { | |
js.context.callMethod('copyHtmlToClipboard', [content]); | |
} | |
@override | |
getBlob(Uint8List wavData) { | |
return html.Blob([wavData]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'web_abstract_class.dart'; | |
void initApp() async { | |
if (kIsWeb) { | |
CustomPlatform().removeLocalStorage(); | |
} | |
runApp(const MyApp()); | |
return; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:typed_data'; | |
import 'platform_helper.dart' | |
if (dart.library.html) 'package:bhsf_provider/core/helpers/common_web_import.dart'; | |
abstract class CustomPlatform { | |
factory CustomPlatform() => getInstance(); | |
dynamic getBlob(Uint8List wavData); | |
void copyHtmlFromWeb(String content); | |
void removeLocalStorage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment