【问题标题】:What does this line "_contacts?.length ?? 0" means in flutter/dart这行“_contacts?.length ?? 0”在颤振/飞镖中是什么意思
【发布时间】:2021-02-25 20:27:01
【问题描述】:

我正在学习在flutter app中使用Contacts,在理解和编写代码的过程中,我看到了这个表达-_contacts?.length ?? 0,
我不明白这是什么意思,这里问号有什么用?

这是完整的代码 -

import 'package:flutter/material.dart';
import 'package:contacts_service/contacts_service.dart';

class ContactsPage extends StatefulWidget {
  @override
  _ContactsPageState createState() => _ContactsPageState();
}

class _ContactsPageState extends State<ContactsPage> {
  Iterable<Contact> _contacts;

  @override
  void initState() {
    getContacts();
    super.initState();
  }

  Future<void> getContacts() async {
    //Make sure we already have permissions for contacts when we get to this
    //page, so we can just retrieve it
    final Iterable<Contact> contacts = await ContactsService.getContacts();
    setState(() {
      _contacts = contacts;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: (Text('Contacts')),
      ),
      body: _contacts != null
          //Build a list view of all contacts, displaying their avatar and
          // display name
          ? ListView.builder(
              itemCount: _contacts?.length ?? 0,
              itemBuilder: (BuildContext context, int index) {
                Contact contact = _contacts?.elementAt(index);
                return ListTile(
                  contentPadding:
                      const EdgeInsets.symmetric(vertical: 2, horizontal: 18),
                  leading: (contact.avatar != null && contact.avatar.isNotEmpty)
                      ? CircleAvatar(
                          backgroundImage: MemoryImage(contact.avatar),
                        )
                      : CircleAvatar(
                          child: Text(contact.initials()),
                          backgroundColor: Theme.of(context).accentColor,
                        ),
                  title: Text(contact.displayName ?? ''),
                  //This can be further expanded to showing contacts detail
                  // onPressed().
                );
              },
            )
          : Center(child: const CircularProgressIndicator()),
    );
  }
}

这是页面的链接 - How to access contacts in flutter

【问题讨论】:

标签: android ios flutter dart


【解决方案1】:

它首先检查_contacts。 如果它不为空,则检查_contacts.length。 如果_contacts.length 不为null,则设置其值,否则设置为零。

【讨论】:

    【解决方案2】:

    _contacts?.length ?? 0

    第一个? 表示如果联系人是null,则不要尝试调用length 并只返回null,因为在null 上调用.length 会引发错误。所以基本上你是在避免 null 指针错误。

    第二个??表示如果contactslengthnull(即??之前的值),则返回0

    The Dart Language Tour - Operators Section 是 Dart 算子的一个很好的参考。

    【讨论】:

      【解决方案3】:

      表示如果_contacts数组为null,则length将得到0

      【讨论】:

        猜你喜欢
        • 2021-02-16
        • 2020-08-31
        • 2019-02-05
        • 1970-01-01
        • 2020-08-15
        • 2020-08-12
        • 2020-11-05
        • 1970-01-01
        • 2020-09-12
        相关资源
        最近更新 更多