Can I disable a specific dependency for Flutter web? - flutter

I'm usingAdMob and Google Mobile Ads package for my flutter app and it works fine on iOS and Android, but as I understood it can't be used on flutter web and when I try to run my code on web it throws an exception for that dependency not being initialized. Is there a way to disable that dependency only for Flutter web? Or if there is a way to implement Google Ads on web?
Thank you

You can do something like below
import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// Do nothing
} else {
// Init Your dependency
}

Related

Flutter RevenueCat SDK not working on web

For my flutter IOS/Android app i use https://pub.dev/packages/purchases_flutter ( RevenueCat ) to manage subscription and it's working perfectly well.
Now i want to export my flutter into a webapp using : Flutter build web
I got that error :
''No implementation found for method setupPurchases on channel purchases_flutter''
That's because purchases_flutter is not made for webapp for now.
My question:
I really need to export my application into a webapp, is there a way i can avoid that kind of error? Can i trick my code to skip every part that refer to that package?
I found that you can put a condition before initializing revenuecat plugin so if you are on the web it skip that part and the application can build correctly
To check if you are on web
import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// running on the web!
} else {
// NOT running on the web! You can check for additional platforms here.
}

Calling platform specific SDK from Flutter

I already have an Android and IOS SDK which can communicate to one of my clients and manipulate data. Is there a way I can call the methods in those SDK from Flutter.
For example: I have a method registerUser(String username) in my Android SDK. Currently I have to use the Flutter channel in a custom Android code and call the method registerUser(String username) from there. This adds overhead as I have to rewrite all my hundreds of methods in the custom code in Flutter or generate a new Android SDK that supports the Flutter calls.
So is there a way I can call the methods in my Android SDK from Flutter directly or using some interface which requires less efforts?
try to use:
import "dart:io" show Platform;
...
if(Platform.isIOS){ ... }
if(Platform.isAndroid){ ... }

How to open Viber app in Flutter web app?

I want to open Viber app (Android / Windows) from flutter web application. I tried using url_launcher but it does not work.
The URL to open Viber chat is viber://chat?number=xxx. How can I call this from flutter web app?
Found a solution. I used html package
import 'dart:html' as html;
After that
html.window.open('viber://chat?number=xxx', '');
You can try this package Flutter AppAvailability Plugin
Searching for the same solution but only for a mobile app, Google gives me only this question. So, I decided to write the research results here. Maybe it will come in handy.
For a mobile app, you can use url_launcher
launch('viber://chat?number=$phoneNumber');
If you want to open the conversation with a predefined text, you can write like this:
launch('viber://chat/?number=$phoneNumber&draft=$yourMessage');
Please note, if Viber is not installed on your device, the launch method throws an error.

Flutter Image picker for web and mobile

I need to develop a cross platform app using flutter that supports mobile as well as web platforms. I need an image picker that works for mobile as well as web. Currently I am using image picker flutter plugin for mobile and flutter_web_image_picker for web. But the build fails as flutter_web_image_picker uses dart:HTML and it is not supported for mobile builds. I need to know that how shall I conditionally render web pickers for their respective platforms as having two separate code bases won't make any sense?
Might be very late, but this is how we can execute any code conditionally for different platforms in flutter.
import 'package:flutter/foundation.dart' show kIsWeb;
Then in the function code -
if(kIsWeb){
... web specific code
} else {
... mobile specific code
}

Flutter - Different click actions for Web and Android in single project

I wanna implement YoutubePlayer in my Flutter project which runs in web and android platforms. I'm using youtube_player_flutter package for Android and embedded YouTube player view in web. How to separate both codes in single project?
You can use the dart:io packages Platform class to check the Platform that is running the code.
See https://api.flutter.dev/flutter/dart-io/Platform-class.html, there exists
Platform.isAndroid.
To check if you are deploying to web, there is a constant called kIsWeb in the flutter foundation. Use can use it like this:
import 'package:flutter/foundation.dart' show kIsWeb;
import 'dart:io' show Platform;
if (kIsWeb) {
// use this for web
} else if (Platform.isAndroid) {
// use this for android
}