Flutter : Class 'Future<List<String>>' 没有实例 getter 'length'
Flutter : Class 'Future<List<String>>' has no instance getter 'length'
我在 Flutter 应用程序的 SharedPreference 上保存了一个字符串列表,我想在 Provider 中调用它并在小部件中使用它。
供应商:
get myTeam => getMyTeam();
Future<List<String>> getMyTeam() async {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
final SharedPreferences prefs = await _prefs;
return prefs.getStringList('team');
}
我在未来的构建器中使用了它:
Widget build(BuildContext context) {
return Consumer<GeneralProvider>(
builder: (context, generalProvider, child) {
var items = generalProvider.myTeam;
return FutureBuilder(
future: items,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${items[index].club}'),
);
});
} else {
return Text('bad');
}
});
});
}
我收到错误:Class 'Future<List<String>>' has no instance getter 'length'
我在这里解决了许多问题,例如:
Class 'Future<dynamic>' has no instance getter 'length'
但没有解决
将itemCount: items.length
改为itemCount: snapshot.length
,
以及 FutureBuilder<List<String>(...etc)
的未来建设者。
最终会变成这样:
Widget build(BuildContext context) {
return Consumer<GeneralProvider>(
builder: (context, generalProvider, child) {
var items = generalProvider.myTeam;
return FutureBuilder<List<String>>(
future: items,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.length,
itemBuilder: (context, index) {
return ListTile(
// title: Text('${items[index].club}'),//This will likely throw an error also, because items is a List<String>, there is no method called "club" for Lists.
//Replace it with this to validate:
title: Text(snapshot[index]),//This
);
});
} else {
return Text('bad');
}
});
});
}
我在 Flutter 应用程序的 SharedPreference 上保存了一个字符串列表,我想在 Provider 中调用它并在小部件中使用它。 供应商:
get myTeam => getMyTeam();
Future<List<String>> getMyTeam() async {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
final SharedPreferences prefs = await _prefs;
return prefs.getStringList('team');
}
我在未来的构建器中使用了它:
Widget build(BuildContext context) {
return Consumer<GeneralProvider>(
builder: (context, generalProvider, child) {
var items = generalProvider.myTeam;
return FutureBuilder(
future: items,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${items[index].club}'),
);
});
} else {
return Text('bad');
}
});
});
}
我收到错误:Class 'Future<List<String>>' has no instance getter 'length'
我在这里解决了许多问题,例如: Class 'Future<dynamic>' has no instance getter 'length'
但没有解决
将itemCount: items.length
改为itemCount: snapshot.length
,
以及 FutureBuilder<List<String>(...etc)
的未来建设者。
最终会变成这样:
Widget build(BuildContext context) {
return Consumer<GeneralProvider>(
builder: (context, generalProvider, child) {
var items = generalProvider.myTeam;
return FutureBuilder<List<String>>(
future: items,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.length,
itemBuilder: (context, index) {
return ListTile(
// title: Text('${items[index].club}'),//This will likely throw an error also, because items is a List<String>, there is no method called "club" for Lists.
//Replace it with this to validate:
title: Text(snapshot[index]),//This
);
});
} else {
return Text('bad');
}
});
});
}