我正在使用contacts_service
软件包将联系人信息从移动设备获取到Flutter应用程序中。我想创建一个包含所有数据的JSON对象,为此,我需要获取名称,电话,地址等详细信息。但是,这样做却出错了。
这是我的获取方式。
Future<void> getContacts() async {
final Iterable<Contact> contacts = await ContactsService.getContacts();
var _contactArray = contacts.toList();
var _contactsJson = [];
for (var i = 0; i < _contactArray.length; i++) {
Contact contact = contacts?.elementAt(i);
Iterable<Item> phone = contact?.phones;
print('single contact phone');
print(phone?.elementAt(0)?.value.toString());
PostalAddress postalAddress = contact?.postalAddresses?.elementAt(0);
String address = postalAddress?.street.toString() +
postalAddress?.region.toString() +
postalAddress?.city.toString() +
postalAddress?.country.toString() +
postalAddress?.postcode.toString();
print('single contact address');
print(address ?? '');
_contactsJson.add(singleContact);
}
}
这是我遇到的错误。
E/flutter (26989): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter (26989): #0 List.[] (dart:core-patch/array.dart:14:52)
E/flutter (26989): #1 ListMixin.elementAt (dart:collection/list.dart:59:33)
E/flutter (26989): #2 MappedListIterable.elementAt (dart:_internal/iterable.dart:417:40)
E/flutter (26989): #3 _ContactsPageState.getContacts
package:delivero_demo_app/contacts_page.dart:49
E/flutter (26989): <asynchronous suspension>
E/flutter (26989): #4 _ContactsPageState.initState
package:delivero_demo_app/contacts_page.dart:29
E/flutter (26989): #5 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4684
E/flutter (26989): #6 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
E/flutter (26989): #7 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #8 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #9 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5874
E/flutter (26989): #10 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #11 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #12 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
E/flutter (26989): #13 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
E/flutter (26989): #14 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4525
E/flutter (26989): #15 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
E/flutter (26989): #16 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #17 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #18 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5874
E/flutter (26989): #19 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #20 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #21 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5874
E/flutter (26989): #22 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #23 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #24 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
E/flutter (26989): #25 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4719
E/flutter (26989): #26 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
E/flutter (26989): #27 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4525
E/flutter (26989): #28 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4710
E/flutter (26989): #29 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
E/flutter (26989): #30 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #31 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #32 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5874
E/flutter (26989): #33 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #34 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #35 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5874
E/flutter (26989): #36 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #37 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #38 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4571
E/flutter (26989): #39 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4719
E/flutter (26989): #40 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
E/flutter (26989): #41 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4525
E/flutter (26989): #42 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4710
E/flutter (26989): #43 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
E/flutter (26989): #44 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
E/flutter (26989): #45 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
E/flutter (26989): #46 C
从单个联系人获取电话和地址的正确方法是什么?任何帮助都会很棒。谢谢
答案 0 :(得分:0)
在单个Contact
上,您可以调用toMap()
方法。
也许是这样的:
Iterable<Contact> _contacts = await ContactsService.getContacts();
List<Contact> _contactList = _contacts.toList();
List _contactMaps = _contactList.map((e) => e.toMap()).toList();
String json = jsonEncode(_contactMaps);
print(json);
可能有更好的方法,但这就是我的工作方式。