problem with launching whatsapp to send a message with flutter url_launcher - flutter

this is the function to send an WhatsApp message (or just launch the WhatsApp with the message)
in the Cipher.dart
void sendCodeByWhatsApp(
String phone,
String message,
) async {
String url() {
if (Platform.isAndroid) {
return "https://wa.me/$phone/?text=$message";
} else {
return "https://api.whatsapp.com/send?phone=$phone=$message";
}
}
if (await canLaunchUrl(Uri.parse(url()))) {
await launchUrl(Uri.parse(url()));
} else {
throw 'Could not launch ${url()}';
}
}
and here I use it:
ElevatedButton(
child: const Icon(Icons.whatsapp, color: Colors.white,),
onPressed: (){
Cipher().sendCodeByWhatsApp(encrypt.encrypt, phone.text);
},
),
when adding a number and message, just open a page with WhatsApp logo, tells me:
we couldn't find the page you were looking for

Refer to this to get how to use the link properly link
There may also be an error if the number includes the country code like
+1 0123456789
Will give an error. The phone should not include the country code.

after doing some search, finally found the solution:
same code above, but the URL should be like this:
"whatsapp://send?phone=$phone&text=${Uri.parse(message)}";
for android, and it is working like a charm...

Change the ? to & and it will open the WhatsApp page in the browser for you
"https://wa.me/$phone/&text=$message";

Related

How to send message on specific number (Restaurant number - For food ordering) on whatsapp from my flutter app?

launchWhatsapp(String mobileNumber,BuildContext context) async {
var whatsapp = mobileNumber;
var whatsappAndroid =Uri.parse("whatsapp://send?phone=$whatsapp&text=hello");
if (await canLaunchUrl(whatsappAndroid)) {
await launchUrl(whatsappAndroid);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("WhatsApp is not installed on the device"),
),
);
}
}
Here, I have used url_launcher plugin,
https://pub.dev/packages/url_launcher
But the mobile number is fixed for all time, mobileNumber = "9876543211"
Now, I want to redirect on whatsapp and open chat on this number for food ordering. so everytime number will be same.
By launchWhatsapp method it redirect me on whatsapp but it shows me that, this number is not registered or saved in your contacts. How do I open chat screen on whatsapp from any device from my flutter app.
Just put a country code as prefix of your mobile number it will work.
var whatsapp = mobileNumber;
I just did mistake of passing only number. with that, you have to pass country code as well.
var whatsapp = "+912345678999";
You have to give it the full correct number containing the country code

NFC Detection doesn't work using NfcManager in Flutter/Dart

first time posting here. Am quite new to flutter.
Currently making an app that uses NFC to allow user login, however I'm unable to show the data of the NFC/print it out so I can use it. I want to be able to print out the code on it that is '0248FCF2255E81', but I can only get the metadata and it doesn't seem to be inside that.
RaisedButton(
onPressed: () async {
bool isAvailable =
await NfcManager.instance.isAvailable();
// Start Session
if (isAvailable) {
NfcManager.instance.startSession(
onDiscovered: (NfcTag tag) async {
Ndef ndef1 = Ndef.from(tag);
if (ndef1 == null) {
print('Tag is not compatible with NDEF');
return;
} else {
print(await ndef1.read());
}
},
);
} else {
print("NFC Manager is not available");
}
},
child: Text('Open NFC Manager'),
color: Colors.green[300],
),
When I click the button, it opens the NfcManager, and I should be able to get the data from the NFC tag. However, when I hover my phone over the NFC Tag, I get this error
NoSuchMethodError (NoSuchMethodError: The method 'forEach' was called on null. Receiver: null Tried calling: forEach(Closure: (dynamic, dynamic) => Null))
I tried other packages like nfc_in_flutter, but that didn't work for me when trying to fork their project
I also tried flutter_nfc_reader, which did give me the metadata of the NFC tag, but not the data I wanted.
Same thing with nfc_manager, when I hover over the tag after the error pops up, I can see the metadata of the NFC tag as well, but not see the string I wanted to see.
Hope I can find some help here, thanks!
edit: The package I'm using is https://pub.dev/packages/nfc_manager
tried nfc_in_flutter again and it worked, using
NDEFMessage message = await NFC.readNDEF(once: true).first;
print("payload: ${message.tag.id}");

mailto: link for Flutter for Web

The url_launcher package (https://pub.dev/packages/url_launcher) doesn't seem to work for Flutter for Web. The following code prints "test url1" but nothing happens afterwards.
How can I implement mailto: like functionality in Flutter for Web which causes the default email app to open with a prepopulated 'to:' email address?
FlatButton(
onPressed: _mailto, //() => {},
padding: EdgeInsets.all(3.0),
child: _contactBtn(viewportConstraints),
)
_mailto() async {
const url = 'mailto:support#email.com?subject=Product Inquiry&body=';
print("test url1");
if (await canLaunch(url)) {
print("test url2");
await launch(url);
} else {
print("test url3");
throw 'Could not launch $url';
}
}
After experimenting a little I found a way to make url_launcher work with web.
Don't use canLaunch(url). Instead, just use launch(url), but wrap it in try-catch block. This way you should be safe and the email link will work. For catch you can just copy the email to clipboard and notify the user about that with a snackbar or smth. Probably not the best solution, but a good one, till we get something better.
Here is the sample code, so that you see what I mean:
void _launchMailClient() async {
const mailUrl = 'mailto:$kEmail';
try {
await launch(mailUrl);
} catch (e) {
await Clipboard.setData(ClipboardData(text: '$kEmail'));
_emailCopiedToClipboard = true;
}
}
import 'package:mailto/mailto.dart';
// For Flutter applications, you'll most likely want to use
// the url_launcher package.
import 'package:url_launcher/url_launcher.dart';
// ...somewhere in your Flutter app...
launchMailto() async {
final mailtoLink = Mailto(
to: ['to#example.com'],
cc: ['cc1#example.com', 'cc2#example.com'],
subject: 'mailto example subject',
body: 'mailto example body',
);
// Convert the Mailto instance into a string.
// Use either Dart's string interpolation
// or the toString() method.
await launch('$mailtoLink');
}

How do I open instagram from flutter app?

I want to transite to Instagram profile, when I am tapping on button. I use this library url_launcher. But there I can use only Web Browser for this. What will I do, in order to reach my goal?
To open Native and WebView Instagram:
Add to your iOS/Runner/Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
...
<string>instagram</string>
</array>
Install url_launcher ( https://pub.dev/packages/url_launcher )
Import
import 'package:url_launcher/url_launcher.dart';
Create some method like this;
Inside Widget for example:
_launchInstagram() async {
const nativeUrl = "instagram://user?username=severinas_app";
const webUrl = "https://www.instagram.com/severinas_app/";
if (await canLaunch(nativeUrl)) {
await launch(nativeUrl);
} else if (await canLaunch(webUrl)) {
await launch(webUrl);
} else {
print("can't open Instagram");
}
}
Well, I don't know if it's too late, but this works for me using the url_launcher library (tested on android only):
var url = 'https://www.instagram.com/<INSTAGRAM_PROFILE>/';
if (await canLaunch(url)) {
await launch(
url,
universalLinksOnly: true,
);
} else {
throw 'There was a problem to open the url: $url';
}
2022 update:
_launchInstagram() async {
var nativeUrl = "instagram://user?username=balkan.exe";
var webUrl = "https://www.instagram.com/balkan.exe";
try {
await launchUrlString(nativeUrl, mode: LaunchMode.externalApplication);
} catch (e) {
print(e);
await launchUrlString(webUrl, mode: LaunchMode.platformDefault);
}
}
I am not sure if you can launch the profile activity, unless you know the name (which might change in the future). But, if you can try launching the app by using Intent plugin:
Try adding the Intent plugin:
https://pub.dev/packages/intent
Add intent in you in your pubspec.yaml file.
You can call the specific app with the package name (in this case Instagram).
Intent()
..setAction(Action.ACTION_SHOW_APP_INFO)
..putExtra(Extra.EXTRA_PACKAGE_NAME, "instagram package name")
..startActivity().catchError((e) => print(e));
You can also make use Android flutter plugin for Intent:
https://pub.dev/packages/android_intent
This will support for Android :
Use it by specifying action, category, data and extra arguments for the intent. It does not support returning the result of the launched activity
For IOS url_launcher plugin can be used for deep linking
You can create a platform channel and use the intent to achieve that or use social_share package.
Install url_launcher ( https://pub.dev/packages/url_launcher )
They now have a mode parameter, which you set to LaunchMode.externalApplication;
Call it with the regular https url, if they have the app installed it will open the app, otherwise it will open in Safari. Don't worry about doing an app link and a website url, the package and the OS handle it accordingly.
For example, here are my links in our About section of the app to our socials..
SettingsListTileModel(
// This can be any website's url that has a corresponding app, we us it with Twitter, Facebook, Instagram, and Youtube.
onTap: () => launchUrl(
Uri.parse('https://www.instagram.com/forwheel_app/'),
mode: LaunchMode.externalApplication,
),
title: Text(
'Instagram',
style: context.bodyLarge),
),
trailing: Icon(
FontAwesomeIcons.squareArrowUpRight,
),
),

Flutter : Sending Email

i want little ask about sending email with flutter . I using https://pub.dev/packages/flutter_email_sender#-readme-tab- for sending email.
Sending Function
Future sendEmail(String subject,String body,List<String> recipients) async{
final Email email = Email(body: body,subject: subject,recipients: recipients);
String platformResponse;
try {
await FlutterEmailSender.send(email);
platformResponse='success';
} catch (e) {
platformResponse = e.toString();
}
print(platformResponse);
}
View.dart
Center(
child: RaisedButton(
onPressed: () => _sendMail(),
child: Text('send'),
),
)
void _sendMail() async {
return await api.sendEmail(widget.namaUpdate, widget.jurusanUpdate,['zefry.reynando#gmail.com']);
}
it's possible sending email automatic without open gmail app first ? (Like in codeigniter)
i trying using another package but always open gmail app first. or this how it works?
Thanks
You're not likely to find a package that sends email out without either configuration or a visible mail client. That app would not by approved by either Apple or Google, because it might be a source of SPAM.