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

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
}

Related

Already using sqflite database for mobile App which is not supporting to WebApp,can i use different library for web App in same if yes than which lib

i am using sqflite library for database but it is supporting to IOS/Android app can i use to database for App existing one(Sqflite) and for webApp different in the same app if I can then how and which will be the best.Thanks
Yes. You can use a separate package for the web. Now, to check whether your app is running on web or not:
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.
}
If your use-case is just to store small values, you can use shared_preferences which is supported for both web and mobile.
You can also try hive which is supported for both.

How to develop for multiple platforms in flutter when some packages are not compatible with some platforms?

I am trying to migrate my existing Flutter app to web. The problem is that certain packages are not compatible with Web, (e.g. path_provider and many others).
I don’t really want to build a whole new project so is there a way to exclude certain dependencies for web? Couldn’t find anything on this.
Yes, you can write platform specific codes, if you flutter app is meant to run on more two platform you need to use some packages for only one platform. This can also be done for all Platforms.
import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// Use packages that are compatible with Web.
} else {
// Use packages that aren't compatible with Web.
}

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.
}

Can I disable a specific dependency for Flutter web?

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
}

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
}