Code Monkey home page Code Monkey logo

flutter_libphonenumber's Introduction

Flutter libphonenumber

Flutter plugin wrapper to Google's libphonenumber

Uses flutter platform channel to communicate with native implementation of libphonenumber.

This library uses below native libraries

NOTE

Only number parsing is implemented as of now

Usage

try{
  final parsed = await FlutterLibPhoneNumber.parse(_phoneController.text);
} on PlatformException catch(e){
  debugPrint(e.code +' --- '+e.message);
}

parse() takes 2 parameters.

    numberString - Number string to be parsed

    defaultRegion - default is IN. Used when country code prefix is not provided with phone number string

flutter_libphonenumber's People

Contributors

hariprasadiit avatar

Stargazers

Kwame Adusei Fosu avatar George Lima avatar  avatar Henrick avatar Andrew Chen avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

alejamp

flutter_libphonenumber's Issues

java.lang.IllegalStateException: Reply already submitted

I have the following service

class ContactsService with ChangeNotifier {
  LoadingState _state = LoadingState.uninitialized;
  List<MobileContactModel> _mobileContacts = List<MobileContactModel>();

  LoadingState get state => _state;
  List<MobileContactModel> get mobileContacs => _mobileContacts.toList();

  ContactsService() {
    refreshContacts();
  }

  Future<void> refreshContacts() async {
    bool hasPermission = await Permission.contacts.request().isGranted;

    if (!hasPermission) {
      _state = LoadingState.permissionDenied;
      notifyListeners();
    }

    _state = LoadingState.loading;
    notifyListeners();

    List<MobileContactModel> rawContacts = (await contactsService.ContactsService.getContacts(withThumbnails: false))
        .where((contact) => contact.phones.any((iterable) => iterable.label == 'mobile'))
        .map((contact) => MobileContactModel(
            contact.displayName, contact.phones.firstWhere((phoneNumber) => phoneNumber.label == 'mobile').value))
        .toList();

    _mobileContacts = await validateContacts(rawContacts);

    _state = LoadingState.loaded;
    notifyListeners();
  }

  Future<List<MobileContactModel>> validateContacts(List<MobileContactModel> rawContacts) async {
    List<Future<MobileContactModel>> validations = List<Future<MobileContactModel>>();

    for (MobileContactModel rawContact in rawContacts) validations.add(_validateContact(rawContact));

    var test = (await Future.wait(validations)).toList();
    test.removeWhere((value) => value == null);
    return test;
  }

  Future<MobileContactModel> _validateContact(MobileContactModel rawContact) async {
    return await _isValidMobileNumber(rawContact.phonenumber) ? rawContact : null;
  }

  Future<bool> _isValidMobileNumber(String phoneNumber) async {
    if (phoneNumber.isEmpty) return false;

    try {
      PhoneNumberType type = await PhoneNumberUtil.getNumberType(phoneNumber: phoneNumber, isoCode: 'NL');
      return type == PhoneNumberType.mobile;
    } catch (error) {
      return false;
    }
  }
}

But the _isValidMobileNumber() keeps throwing the following error. Is there a way to catch this error? Is there anything I can do about this? Or is it an error that's in the package?

E/MethodChannel#codeheadlabs.com/libphonenumber(13840): Failed to handle method call
E/MethodChannel#codeheadlabs.com/libphonenumber(13840): java.lang.IllegalStateException: Reply already submitted
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:148)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:234)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at com.codeheadlabs.libphonenumber.LibphonenumberPlugin.formatAsYouType(LibphonenumberPlugin.java:157)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at com.codeheadlabs.libphonenumber.LibphonenumberPlugin.onMethodCall(LibphonenumberPlugin.java:44)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#codeheadlabs.com/libphonenumber(13840):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/DartMessenger(13840): Uncaught exception in binary message listener
E/DartMessenger(13840): java.lang.IllegalStateException: Reply already submitted
E/DartMessenger(13840):         at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:148)
E/DartMessenger(13840):         at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:249)
E/DartMessenger(13840):         at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/DartMessenger(13840):         at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/DartMessenger(13840):         at android.os.MessageQueue.nativePollOnce(Native Method)
E/DartMessenger(13840):         at android.os.MessageQueue.next(MessageQueue.java:336)
E/DartMessenger(13840):         at android.os.Looper.loop(Looper.java:174)
E/DartMessenger(13840):         at android.app.ActivityThread.main(ActivityThread.java:7356)
E/DartMessenger(13840):         at java.lang.reflect.Method.invoke(Native Method)
E/DartMessenger(13840):         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/DartMessenger(13840):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.