简体   繁体   English

我正在尝试将联系人从模拟器获取到下拉列表,并让用户转到 select 之一。 我收到一个错误

[英]I'm trying to get the contacts from the emulator to a dropdownlist and let the user to select one. I'm getting an error

Probably the contacts have duplicates, but I want the duplicates to be accepted.可能联系人有重复项,但我希望接受重复项。 The basic idea is to access the contact list and populate the values to a dropdownMenu and let the user to select a contact from there and save to a file.基本思想是访问联系人列表并将值填充到下拉菜单中,让用户从那里到 select 联系人并保存到文件中。 I have already initialised the dropdownMenu with a string "Select a contact" through a variable.我已经通过变量用字符串“选择联系人”初始化了下拉菜单。

Exception has occurred.
_AssertionError ('package:flutter/src/material/dropdown.dart': Failed assertion: line 890 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1': There should be exactly one item with [DropdownButton]'s value: Select a contact. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)

Here is the complete code这是完整的代码

import 'package:flutter/material.dart';
import 'package:contacts_service/contacts_service.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:io';
import 'dart:convert';
import 'package:url_launcher/url_launcher.dart';
import 'package:path_provider/path_provider.dart';

class Interface extends StatelessWidget {
  const Interface({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('pAM'),
      ),
      body: const ContactSelector(),
    );
  }
}

class ContactSelector extends StatefulWidget {
  const ContactSelector({super.key});

  @override
  _ContactSelectorState createState() => _ContactSelectorState();
}

class _ContactSelectorState extends State<ContactSelector> {
  Contact _selectedContact = Contact();
  late bool _isTrue;
  late Iterable<Contact> _contacts;
  List<DropdownMenuItem<String>> _dropdownItems = [];
  String _selectedName = "Select Contact";
  //late List<DropdownMenuItem<String>> _dropdownItems;

  @override
  void initState() {
    super.initState();
    _getContacts();
    _selectedName = _dropdownItems.isNotEmpty
        ? _dropdownItems[0].value!
        : 'Select a contact';
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        if (_dropdownItems != null)
          DropdownButton<String>(
            value: _selectedName,
            items: _dropdownItems,
            onChanged: (newValue) {
              _onContactChanged(newValue!);
            },
          )
        else
          const Text("Loading...")
      ],
    );
  }

  String? encodeQueryParameters(Map<String, String> params) {
    return params.entries
        .map((e) =>
            '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
        .join('&');
  }

  void _sendMessage(String message) async {
    String phoneNumber = _selectedContact.phones.toString();
    Uri uri = Uri(
      scheme: 'sms',
      path: phoneNumber,
      query: encodeQueryParameters(<String, String>{
        'body': 'Welcome to pAM',
      }),
    );

    if (await canLaunchUrl(uri)) {
      await canLaunchUrl(uri);
    } else {
      throw 'Could not send SMS';
    }
  }

  _getContacts() async {
    _contacts = await ContactsService.getContacts(withThumbnails: false);
    _dropdownItems = _contacts
        .map((c) => DropdownMenuItem(
              value: c.displayName,
              child: Text(c.displayName.toString()),
            ))
        .toList();
    setState(() {});
  }

  _onContactChanged(String newValue) {
    setState(() {
      _selectedName = newValue;
      _selectedContact =
          _contacts.firstWhere((c) => c.displayName == _selectedName);
    });
    _saveContactToFile(_selectedContact);
    _readJson();
  }

  _saveContactToFile(Contact contact) async {
    final directory = await getApplicationDocumentsDirectory();
    final file = File('${directory.path}/selected_contact.txt');
    if (!(await file.exists())) {
      file.create();
    }
    file.writeAsString(jsonEncode(contact.toMap()));
  }

  void _readJson() async {
    final directory = await getApplicationDocumentsDirectory();
    final file = File('${directory.path}/true.json');
    if (await file.exists()) {
      final content = jsonDecode(await file.readAsString());
      if (content["isTrue"]) {
        _promptMessage();
      } else {
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: const Text('Reminder'),
                content: const Text(
                    "You can continue your work, remember your loved ones misses you"),
                actions: <Widget>[
                  ElevatedButton(
                    child: const Text('OK'),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            });
      }
    }
  }

  _promptMessage() {
    if (_isTrue) {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text('Select a message'),
            content: SingleChildScrollView(
              child: ListBody(
                children: <Widget>[
                  InkWell(
                      child: const Text('How are you?'),
                      onTap: () {
                        _sendMessage('How are you?');
                      }),
                  InkWell(
                      child: const Text('What are you up to?'),
                      onTap: () {
                        _sendMessage('What are you up to?');
                      }),
                  InkWell(
                      child: const Text('What is for dinner?'),
                      onTap: () {
                        _sendMessage('What is for dinner?');
                      }),
                ],
              ),
            ),
            actions: <Widget>[
              ElevatedButton(
                child: const Text('Cancel'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    }
  }
}

This is the key part of the error message: There should be exactly one item with [DropdownButton]'s value: Select a contact.这是错误消息的关键部分: There should be exactly one item with [DropdownButton]'s value: Select a contact. You are setting the value of the DropdownButton to "Select a contact" (presumably because _dropdownItems.isNotEmpty == false ), but none of the DropdownMenuItem s that you have given to the DropdownButton via its items property has "Select a contact" as its value.您正在将DropdownButtonvalue设置为"Select a contact" (大概是因为_dropdownItems.isNotEmpty == false ),但是您通过其items属性提供给DropdownButtonDropdownMenuItem都没有"Select a contact"作为其价值。 You might want to look into the use of the hint property to show the "Select a contact" , well, hint.您可能想查看hint属性的使用,以显示"Select a contact" ,好吧,提示。

Something like the (untested) code below:类似于下面的(未经测试的)代码:

DropdownButton<String>(
            hint: Text("Select a contact")
            value: _dropdownItems.isNotEmpty ? _dropdownItems.first : null,
            items: _dropdownItems,
            onChanged: (newValue) {
              _onContactChanged(newValue!);
            },
          )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试注册用户时出现意外错误? - While trying to register user i'm getting unexpected error? 我正在尝试在下拉列表中放置一个 var - I'm trying to put a var in dropdownlist 我正在尝试通过 VS 代码在模拟器上运行我的 flutter 应用程序,但收到错误消息,说它找不到我的 main.dart 文件的路径 - I'm trying to run my flutter app on an emulator from VS code and I get an error saying it cannot find the path to my main.dart file 我试图在 flutter 中添加 fonts,但出现错误,我找不到原因 - I'm trying to add fonts in flutter, but I'm getting an error, I couldn't find why 我正在尝试为我的移动应用程序获取 firebase 云消息传递,但我不断收到平台异常错误 - I'm trying to get firebase cloud messaging for my mobile app but I keep getting platform exception error 我正在尝试制作一个简单的应用程序,它可以随机改变身体背景的颜色,但我在模拟器上得到的只是一片空白 - I'm trying to make a simple app that randomly changes the color of the body's background but all I'm getting on the emulator is a blank 我试图在构造函数中创建一个可选参数,但仍然出现错误,有人可以帮助我吗? - I'm trying to create a optional parameter in constructor but still getting error, can any one help me with that? 我在尝试注册用户时遇到错误 - I'm getting a error when I try to Sign up user 我正在尝试将从一个 api ea 组件获得的信息传递给 flutter 中的另一个组件 - I'm trying to pass on information that I get from one api e a component to another in flutter 我试图在按下时在 setState() 中设置一个变量,但出现错误 - I'm trying to set a variable in setState() on press but I'm getting an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM