类'Future <dynamic>'没有实例获取方法'length'

时间:2020-08-29 03:50:37

标签: dart

我正在尝试让LayoutBuilder使用我的API中的某些项目。

使用此代码:

class _ContactsScreenState extends State<ContactsScreen> {
  final _contacts = _dummyData();
  final _selection = ValueNotifier<ResumenPublicaciones>(null);

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

  @override
  Widget build(BuildContext context) {
    var futureBuilder = new FutureBuilder(
      future: _dummyData(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return new Text('loading...');
          default:
            if (snapshot.hasError)
              return new Text('Error: ${snapshot.error}');
            else
              return buildLayoutBuilder();
        }
      },
    );

    return new Scaffold(
      body: futureBuilder,
    );
  }

  LayoutBuilder buildLayoutBuilder() {
    return LayoutBuilder(builder: (context, dimens) {
      if (dimens.maxWidth >= kTabletBreakpoint) {
        const kListViewWidth = 300.0;
        return Row(
          children: <Widget>[
            Container(
              width: kListViewWidth,
              child: buildListView((val) {
                _selection.value = val;
              }),
            ),
            VerticalDivider(width: 0),
            Expanded(
                child: ValueListenableBuilder<ResumenPublicaciones>(
              valueListenable: _selection,
              builder: (context, contact, child) {
                if (contact == null) {
                  return Scaffold(
                    appBar: AppBar(),
                    body: Center(child: Text('No Contact Selected')),
                  );
                }
                return ContactDetails(contact: contact);
              },
            ))
          ],
        );
      }
      return buildListView((val) {
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (_) => ContactDetails(contact: val),
          ),
        );
      });
    });
  }

  Widget buildListView(ValueChanged<ResumenPublicaciones> onSelect) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: false,
        title: Text('Contacts'),
      ),
      body: ListView.separated(
        separatorBuilder: (context, index) => Divider(height: 0),
        itemCount: _contacts.length,
        itemBuilder: (context, index) {
          final _contact = _contacts[index];
          return ListTile(
            leading: Icon(Icons.person),
            title: Text(_contact.name),
            subtitle: Text(_contact.count),
            onTap: () => onSelect(_contact),
          );
        },
      ),
    );
  }
}

_dummyData() async {
  var res = await fetchJobs(http.Client());

  return res;
}

我收到此错误:类'Future'没有实例获取方法'length'。

关于如何执行此操作的任何想法?根据我所读的内容,我需要做一些类似var _contact = Await dummyData()的事情。但是我不确定应该在哪里。

1 个答案:

答案 0 :(得分:1)

您不能在字段初始化程序或构造函数中等待。最好的选择是将_contacts的类型保留为Future<Object>,然后在使用值的地方等待它:

  itemCount: (await _contacts).length

这使您的构建方法变得异步,我认为这是Flutter的问题。您可能需要使用FutureBuilder