簡體   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

可能聯系人有重復項,但我希望接受重復項。 基本思想是訪問聯系人列表並將值填充到下拉菜單中,讓用戶從那里到 select 聯系人並保存到文件中。 我已經通過變量用字符串“選擇聯系人”初始化了下拉菜單。

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)

這是完整的代碼

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();
                },
              ),
            ],
          );
        },
      );
    }
  }
}

這是錯誤消息的關鍵部分: There should be exactly one item with [DropdownButton]'s value: Select a contact. 您正在將DropdownButtonvalue設置為"Select a contact" (大概是因為_dropdownItems.isNotEmpty == false ),但是您通過其items屬性提供給DropdownButtonDropdownMenuItem都沒有"Select a contact"作為其價值。 您可能想查看hint屬性的使用,以顯示"Select a contact" ,好吧,提示。

類似於下面的(未經測試的)代碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM