Flutter can''t send sms using fluttersms - flutter

I'm trying to send sms after my app scanned a qrcode that contains a phone number.
I'm using FlutterSMS package for this but it seems doesn't work as supposed to do, it is not sending the sms. any help?
void _sendSMS(String message, List<String> recipents) async {
String _result =
await FlutterSms.sendSMS(message: message, recipients: recipents)
.catchError((onError) {
print(onError);
});
print(_result);
}
Future _scan() async {
String barcode = await scanner.scan();
List data = barcode.split("|");
String id = data[0];
String phoneNumber = data[1];
String message = "Halo! Pakaian anda sudah selesai di proses, silahkan ambil di gerai kami -LaundryKu";
setState(() {
this.id = id;
this.barcode = barcode;
this.phoneNumber = phoneNumber;
});
_sendSMS(message, data[1]);
}

Brother there is little mistake
second argument is List
_sendSMS(message, recipents);
So you need to Add you Contact Number to List
List<String> recipents = new List();
recipents.add(data[1].toString());
/// then the Msg in Variable
String message = "This is a test message!";
Final pass both variable as parameters in function
_sendSMS(message, recipents);
Finally you are Done ;)

Related

Trying to wrap some flutter code in a try catch block and it errors out

Trying to wrap some flutter code in a try catch block and it errors out.
I get the error "Expected a class member. Try placing this code inside a class member"
Below is mu code snippet
``import 'package:http/http.dart';
import 'dart:convert';
class WorldTime {
String location; //location name for the UI
late String time; //time in that location
String flag; //url to asset flag icon
String url; //location url for api endpoint
WorldTime(this.location, this.time, this.flag, this.url);
try{
Future<void> getTime() async {
Response response = await get(
Uri.parse('http://worldtimeapi.org/api/timezone/$url'));
Map data = jsonDecode(response.body);
//print(data);
String dateTime = data['datetime'];
String offSet = data['utc_offset'].substring(1, 3);
DateTime now = DateTime.parse(dateTime);
now = now.add(Duration(hours: int.parse(offSet)));
time = now.toString();
}
}
catch(e){
}
}`
`
I've looked at examples online to figure out what i might be doing wrong, but nothing jumps out at me. I'm new to flutter though.
Change your code like this,
Future<void> getTime() async {
try{
Response response = await get(
Uri.parse('http://worldtimeapi.org/api/timezone/$url'));
Map data = jsonDecode(response.body);
//print(data);
String dateTime = data['datetime'];
String offSet = data['utc_offset'].substring(1, 3);
DateTime now = DateTime.parse(dateTime);
now = now.add(Duration(hours: int.parse(offSet)));
time = now.toString();
}catch(e){
print(e);
}
}
The try catch block should be inside the method getTime().

Can not send transaction on polygon network using flutter

I am using web3dart to send transaction.
I already connect wallet for polygon but can't sign transaction.
I think it's due to credential problem.
#override
Future<String?> sendAmount({
required String recipientAddress,
required double amount,
}) async {
final sender =
EthereumAddress.fromHex(_connector.connector.session.accounts[0]);
// final recipient = EthereumAddress.fromHex(address);
final recipient = EthereumAddress.fromHex(recipientAddress);
final etherAmount =
EtherAmount.fromUnitAndValue(EtherUnit.szabo, (amount).toInt());
final transaction = Transaction(
to: recipient,
from: sender,
gasPrice: EtherAmount.inWei(BigInt.one),
maxGas: 100000,
value: etherAmount,
);
final credentials = WalletConnectEthereumCredentials(provider: _provider);
// Sign the transaction
try {
final txBytes = await _polygon.sendTransaction(credentials, transaction);
return txBytes;
} catch (e) {
print('Error: $e');
}
return null;
}
This code works on ethereum network but not in polygon.

Dart/Flutter: not working function http.get

I am very new to Flutter, and stuck at the following problem:
My function to get a list of books using googleapis:
Future cercaLibri(String ricerca) async {
final String dominio = 'www.googleapis.com';
final String percorso = '/books/v1/volumes';
Map<String, dynamic> parametri = {'q': ricerca};
final Uri url = Uri.https(dominio, percorso, parametri);
print('hello');
http.get(url).then((res) {
final resJson = json.decode(res.body);
final libriMap = resJson['items'];
libri = libriMap.map<Libro>((value) => Libro.fromMap(value)).toList();
setState(() {
libri = libri;
risultato = res.body;
});
});
setState(() {
risultato = "Caricamento in corso";
});
}
for all files see stackblitz
In pubspec.yaml I have
http: ^0.13.4
It seems to me that the get method is not being called, or maybe the call is pending
Sorry the code isn't in English
try adding "http" or "https" to dominio, like:
final String dominio = "http://www.googleapis.com"
or
final String dominio = "https://www.googleapis.com"
I solved the problem
call effect it works
but it fails 'immagineCopertina' which sometimes returns null
I added a control in Image.newtork by putting a dummy url if 'immagineCopertina' is null

How to get contact number from contact_service Flutter

I'm useing contact_service to manage my contacts. I have fetch all contacts as a list. And I want to access fields with phone number of each contact. I want to get it as a string but the atribute in Contact class is
Iterable<Item> phones
Do You know how can I get a phone number from this ?
Link to this package :
https://pub.dev/packages/contacts_service
Thanks in advance
For every Iterable<Item> phones, Item.value returns the phone number string.
List<String> names = [];
List<String> phones = [];
Iterable<Contact> _contacts = await ContactsService.getContacts(withThumbnails: false);
_contacts.forEach((contact) {
contact.phones.toSet().forEach((phone) {
names.add(contact.displayName ?? contact.givenName);
phones.add(phone.value);
});
});
List<Contact> _contacts;
Future<void> refreshContacts() async {
// Load without thumbnails initially.
var contacts = (await ContactsService.getContacts(
withThumbnails: false, iOSLocalizedLabels: iOSLocalizedLabels))
.toList();
setState(() {
_contacts = contacts;
});
}
and use this contact list to render widget inside ListView/Column
Follow complete example here
https://github.com/lukasgit/flutter_contacts/blob/master/example/lib/contacts_list_page.dart

Dart List doesnt get updated with forEach loop

I am using this package to retrieve device's contacts. The lib retrieve 427 contacts and I want to loop the whole list so that I can create another list and send it to the back-end. The problem is looping does not work this the function return before looping is completed.
Here the function I use:
Future<QueryResult> uploadContacts() async {
final List<Contact> rawContacts =
(await ContactsService.getContacts(withThumbnails: false)).toList();
List<ContactInput> contactsListInput;
print('contactsListInput length: ${rawContacts.length}');
rawContacts.forEach((contact) {
print('contact: $contact'); //PRINTED JUST ONCE
//Contact can have more than 1 number. We need them all
contact.phones.forEach((phone) {
final contactInput =
ContactInput(name: contact.displayName, phone: phone.value);
contactsListInput.add(contactInput);
});
});
print('contactsListInput length: ${contactsListInput.length}'); //NEVER PRINT ANYTHING
final ContactsListInput input =
ContactsListInput(contacts: contactsListInput);
final MutationOptions _options = MutationOptions(
document: SyncContactsMutation().document,
variables: SyncContactsArguments(input: input).toJson());
return client.mutate(_options);
}
I have also tried using for loop and the same thing happened.
for (int i = 0; i < rawContacts.length; i++) {
final contact = rawContacts[i];
final contactInput =
ContactInput(name: contact.displayName, phone: contact.phones.first.value);
contactsListInput.add(contactInput);
}
print('contactsListInput length: ${contactsListInput.length}'); //NEVER CALLED
And I also tried Future.forEach
await Future.forEach(rawContacts, (contact) async {
print('contact: $contact');
//Since contact can have more than one number we loop them too.
await Future.forEach(contact.phones, (phone) async {
final contactInput =
ContactInput(name: contact.displayName, phone: phone.value);
contactsListInput.add(contactInput);
});
});
How to fix this? Any help will be much appreciated.
I have fixed it as
Future<QueryResult> uploadContacts() async {
final Iterable<Contact> rawContacts =
(await ContactsService.getContacts(withThumbnails: false));
final Iterable<ContactInput> contacts = rawContacts.expand((contact) => contact.phones.map(
(phone) =>
ContactInput(name: contact.displayName, phone: phone.value)));
final input = ContactsListInput(contacts: contacts);
final MutationOptions _options = MutationOptions(
document: SyncContactsMutation().document,
variables: SyncContactsArguments(input: input).toJson());
return client.mutate(_options);
}
Credit goes to #pskink and #loganrussell48
You should use it as a dynamic type object. Try something like this:
(event.snapshot.value as dynamic).forEach()
Try and see if it works.