contacts_serviceとcontacts_pluginを使用しようとしたアプリに連絡先を表示したいのですが、パッケージのインストール後に実行するとアプリがクラッシュします。ファイルをインポートしたばかりで、コードを記述していません。ログも表示されません。
私のアプリに連絡先を統合する他の方法も大いに役立ちます!
Flutterアプリケーションの開発に使用する必要のあるツールを学び、習得する必要があります。
そして、SOに質問を投稿するときは、自分のニーズをよりよく説明する必要があります。
つまり、contacts_pluginとcontact_serviceを簡単に試してみました。
両方を使用しないでください。同じ目的で作られています。どちらかを選択してください。
contact_service
より安定しているようです:82を超える92の加重スコア。
アンドロイド
使用しcontacts_plugin
てみると、このエラーが発生したと思います。
* What went wrong:
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.2.51 and higher. Project 'contacts_plugin' is using version 1.2.30.
これは、contacts_plugin
kotlinバージョン1.2.30
を使用していて、私のようにAS> = 3.2を使用しているためです。
ext.kotlin_version = '1.2.30'
。
これを試してみたい場合は、プロジェクトのクローンを作成し、次のように依存関係を含めることができます。
contacts_plugin:
path: ../flutter-contacts-plugin/
プラグインbuild.gradleを次の行に変更します。
ext.kotlin_version = '1.2.30'
これとともに
ext.kotlin_version = '1.2.51'
を使用すると、iOSプロジェクトでも問題が発生しますcontact_plugins
。
contact_serivce
代わりに、AndroidとiOSのどちらでも正常に動作します。
とにかく、Androidではこの権限を自分に追加する必要があることを常に覚えておいてください AndroidManifest.xml
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
iOSの場合は、必ずファイルに設定NSContactsUsageDescription
してくださいInfo.plist
<key>NSContactsUsageDescription</key>
<string>This app requires contacts access to function properly.</string>
そして、SwiftをサポートするFlutterプロジェクトを作成します。
プラグインが提供するデフォルトの例を使用して、contact_service
学習を開始できます。
import 'package:flutter/material.dart';
import 'package:contacts_service/contacts_service.dart';
void main() => runApp(ContactsExampleApp());
class ContactsExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(routes: <String, WidgetBuilder>{
'/add': (BuildContext context) => AddContactPage()
}, home: ContactListPage());
}
}
class ContactListPage extends StatefulWidget {
@override
_ContactListPageState createState() => _ContactListPageState();
}
class _ContactListPageState extends State<ContactListPage> {
Iterable<Contact> _contacts;
@override
initState() {
super.initState();
refreshContacts();
}
refreshContacts() async {
var contacts = await ContactsService.getContacts();
setState(() {
_contacts = contacts;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Contacts Plugin Example')),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Navigator.of(context).pushNamed("/add").then((_) {
refreshContacts();
});
}),
body: SafeArea(
child: _contacts != null
? ListView.builder(
itemCount: _contacts?.length ?? 0,
itemBuilder: (BuildContext context, int index) {
Contact c = _contacts?.elementAt(index);
return ListTile(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
ContactDetailsPage(c)));
},
leading: (c.avatar != null && c.avatar.length > 0)
? CircleAvatar(backgroundImage: MemoryImage(c.avatar))
: CircleAvatar(
child: Text(c.displayName.length > 1
? c.displayName?.substring(0, 2)
: "")),
title: Text(c.displayName ?? ""),
);
},
)
: Center(child: CircularProgressIndicator()),
),
);
}
}
class ContactDetailsPage extends StatelessWidget {
ContactDetailsPage(this._contact);
final Contact _contact;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:
AppBar(title: Text(_contact.displayName ?? ""), actions: <Widget>[
FlatButton(
child: Icon(Icons.delete),
onPressed: () {
ContactsService.deleteContact(_contact);
})
]),
body: SafeArea(
child: ListView(
children: <Widget>[
ListTile(
title: Text("Name"),
trailing: Text(_contact.givenName ?? "")),
ListTile(
title: Text("Middle name"),
trailing: Text(_contact.middleName ?? "")),
ListTile(
title: Text("Family name"),
trailing: Text(_contact.familyName ?? "")),
ListTile(
title: Text("Prefix"), trailing: Text(_contact.prefix ?? "")),
ListTile(
title: Text("Suffix"), trailing: Text(_contact.suffix ?? "")),
ListTile(
title: Text("Company"),
trailing: Text(_contact.company ?? "")),
ListTile(
title: Text("Job"), trailing: Text(_contact.jobTitle ?? "")),
AddressesTile(_contact.postalAddresses),
ItemsTile("Phones", _contact.phones),
ItemsTile("Emails", _contact.emails)
],
),
));
}
}
class AddressesTile extends StatelessWidget {
AddressesTile(this._addresses);
final Iterable<PostalAddress> _addresses;
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ListTile(title: Text("Addresses")),
Column(
children: _addresses
.map((a) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: <Widget>[
ListTile(
title: Text("Street"),
trailing: Text(a.street)),
ListTile(
title: Text("Postcode"),
trailing: Text(a.postcode)),
ListTile(
title: Text("City"), trailing: Text(a.city)),
ListTile(
title: Text("Region"),
trailing: Text(a.region)),
ListTile(
title: Text("Country"),
trailing: Text(a.country)),
],
),
))
.toList())
]);
}
}
class ItemsTile extends StatelessWidget {
ItemsTile(this._title, this._items);
final Iterable<Item> _items;
final String _title;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ListTile(title: Text(_title)),
Column(
children: _items
.map((i) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ListTile(
title: Text(i.label ?? ""),
trailing: Text(i.value ?? ""))))
.toList())
]);
}
}
class AddContactPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _AddContactPageState();
}
class _AddContactPageState extends State<AddContactPage> {
Contact contact = Contact();
PostalAddress address = PostalAddress(label: "Home");
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add a contact"),
actions: <Widget>[
FlatButton(
onPressed: () {
_formKey.currentState.save();
contact.postalAddresses = [address];
ContactsService.addContact(contact);
Navigator.of(context).pop();
},
child: Icon(Icons.save, color: Colors.white))
],
),
body: Container(
padding: EdgeInsets.all(12.0),
child: Form(
key: _formKey,
child: ListView(
children: <Widget>[
TextFormField(
decoration: const InputDecoration(labelText: 'First name'),
onSaved: (v) => contact.givenName = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Middle name'),
onSaved: (v) => contact.middleName = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Last name'),
onSaved: (v) => contact.familyName = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Prefix'),
onSaved: (v) => contact.prefix = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Suffix'),
onSaved: (v) => contact.suffix = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Phone'),
onSaved: (v) =>
contact.phones = [Item(label: "mobile", value: v)],
keyboardType: TextInputType.phone),
TextFormField(
decoration: const InputDecoration(labelText: 'E-mail'),
onSaved: (v) =>
contact.emails = [Item(label: "work", value: v)],
keyboardType: TextInputType.emailAddress),
TextFormField(
decoration: const InputDecoration(labelText: 'Company'),
onSaved: (v) => contact.company = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Job'),
onSaved: (v) => contact.jobTitle = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Street'),
onSaved: (v) => address.street = v),
TextFormField(
decoration: const InputDecoration(labelText: 'City'),
onSaved: (v) => address.city = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Region'),
onSaved: (v) => address.region = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Postal code'),
onSaved: (v) => address.postcode = v),
TextFormField(
decoration: const InputDecoration(labelText: 'Country'),
onSaved: (v) => address.country = v),
],
)),
),
);
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加