gpt4 book ai didi

dart - 类 'Future'没有实例获取方法 'length'

转载 作者:行者123 更新时间:2023-12-03 03:31:30 25 4
gpt4 key购买 nike

我试图让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()的事情。但是我不确定应该在哪里。

最佳答案

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

  itemCount: (await _contacts).length
这使您的构建方法变得异步,我认为这是Flutter的问题。您可能需要使用 FutureBuilder

关于dart - 类 'Future<dynamic>'没有实例获取方法 'length',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63643229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com