flutter addAll 没有为 Iterable 定义
flutter addAll isn't defined for Iterable
第 22 行的以下问题需要您的帮助。
Flutter 说,“addAll 不是为 Iterable 定义的”。我需要更改我的代码中的哪些内容,或者您需要其他信息?
import 'package:flutter/material.dart';
import 'package:MyApp/jsonApi/dataModels/dataModelPlaces.dart';
import 'package:MyApp/jsonApi/parsers/repositoryPlaces.dart';
class ShareAppScreen extends StatefulWidget {
@override
_ShareAppScreenState createState() => _ShareAppScreenState();
}
class _ShareAppScreenState extends State<ShareAppScreen> {
//List<DataModelPlaces> _places = <DataModelPlaces>[];
Iterable<DataModelPlaces> _places = <DataModelPlaces>[];
Iterable<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];
bool _isPlacesLoading = false;
@override
void initState() {
super.initState();
fetchPlaces().then((pvalue) {
setState(() {
_isPlacesLoading = false;
_places.addAll(pvalue); //<- here I have a problem that addAll isn't defined for Iterable
_placesDisplay = _places;
print('User display pin Records: ${pvalue.data!.length}');
var i=0;
while (i < pvalue.data!.length){
print('User display Lat of $i: ${pvalue.data![i].attributes!.latitude}');
print('User display Long of $i: ${pvalue.data![i].attributes!.latitude}');
i++;
}
});
});
}
List stocksList = [
CompanyStocks(name: "Intel Corp", price: 56.96),
CompanyStocks(name: "HP Inc", price: 32.43),
CompanyStocks(name: "Apple Inc", price: 133.98),
CompanyStocks(name: "Microsoft Corporation", price: 265.51)
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Users List ${_places.length}'),
),
body: SafeArea(
child: Container(
child: ListView.builder(
itemCount: stocksList.length,
itemBuilder: (context, index) {
return Card(
child: Padding(
padding: EdgeInsets.all(10),
child: ListTile(
title: Text(
stocksList[index].name,
style: TextStyle(
fontSize: 20,
),
),
leading: CircleAvatar(
child: Text(
stocksList[index].name[0],
style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
),
trailing: Text("$ ${stocksList[index].price}"),
),
),
);
},
),
),
),
);
}
}
class CompanyStocks {
String name;
double price;
CompanyStocks({required this.name,required this.price});
}
最后,我需要 DataModelPlaces 的变量“_places”和“_placesDisplay”,我可以使用它们来代替工作但不是 _places / _placesDisplay 的列表“stocksList”
非常感谢
罗马
Iterable 没有 .addAll
。 您需要先将其转换为 List
,以便您可以向其中添加所有元素,例如:
更新:我的错。 toList()
将 return 一个新的列表!您应该尝试下面的替代方法
// This wrong since it will return a new list (wrong)
// _places.toList().addAll(pvalue)
或者,您可以将定义更改为 List
而不是 Iterable
:
来自:
Iterable<DataModelPlaces> _places = <DataModelPlaces>[];
Iterable<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];
收件人:
List<DataModelPlaces> _places = <DataModelPlaces>[];
List<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];
更新:
如评论中所述,您要确保 fetchPlaces
是 returning 一个 Iterable
以便使用 _places.addAll(pvalue)
否则,如果它是单个对象,使用 _places.add(pvaule)
.
第 22 行的以下问题需要您的帮助。 Flutter 说,“addAll 不是为 Iterable 定义的”。我需要更改我的代码中的哪些内容,或者您需要其他信息?
import 'package:flutter/material.dart';
import 'package:MyApp/jsonApi/dataModels/dataModelPlaces.dart';
import 'package:MyApp/jsonApi/parsers/repositoryPlaces.dart';
class ShareAppScreen extends StatefulWidget {
@override
_ShareAppScreenState createState() => _ShareAppScreenState();
}
class _ShareAppScreenState extends State<ShareAppScreen> {
//List<DataModelPlaces> _places = <DataModelPlaces>[];
Iterable<DataModelPlaces> _places = <DataModelPlaces>[];
Iterable<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];
bool _isPlacesLoading = false;
@override
void initState() {
super.initState();
fetchPlaces().then((pvalue) {
setState(() {
_isPlacesLoading = false;
_places.addAll(pvalue); //<- here I have a problem that addAll isn't defined for Iterable
_placesDisplay = _places;
print('User display pin Records: ${pvalue.data!.length}');
var i=0;
while (i < pvalue.data!.length){
print('User display Lat of $i: ${pvalue.data![i].attributes!.latitude}');
print('User display Long of $i: ${pvalue.data![i].attributes!.latitude}');
i++;
}
});
});
}
List stocksList = [
CompanyStocks(name: "Intel Corp", price: 56.96),
CompanyStocks(name: "HP Inc", price: 32.43),
CompanyStocks(name: "Apple Inc", price: 133.98),
CompanyStocks(name: "Microsoft Corporation", price: 265.51)
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Users List ${_places.length}'),
),
body: SafeArea(
child: Container(
child: ListView.builder(
itemCount: stocksList.length,
itemBuilder: (context, index) {
return Card(
child: Padding(
padding: EdgeInsets.all(10),
child: ListTile(
title: Text(
stocksList[index].name,
style: TextStyle(
fontSize: 20,
),
),
leading: CircleAvatar(
child: Text(
stocksList[index].name[0],
style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
),
trailing: Text("$ ${stocksList[index].price}"),
),
),
);
},
),
),
),
);
}
}
class CompanyStocks {
String name;
double price;
CompanyStocks({required this.name,required this.price});
}
最后,我需要 DataModelPlaces 的变量“_places”和“_placesDisplay”,我可以使用它们来代替工作但不是 _places / _placesDisplay 的列表“stocksList”
非常感谢
罗马
Iterable 没有 .addAll
。 您需要先将其转换为 List
,以便您可以向其中添加所有元素,例如:
更新:我的错。 toList()
将 return 一个新的列表!您应该尝试下面的替代方法
// This wrong since it will return a new list (wrong)
// _places.toList().addAll(pvalue)
或者,您可以将定义更改为 List
而不是 Iterable
:
来自:
Iterable<DataModelPlaces> _places = <DataModelPlaces>[];
Iterable<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];
收件人:
List<DataModelPlaces> _places = <DataModelPlaces>[];
List<DataModelPlaces> _placesDisplay = <DataModelPlaces>[];
更新:
如评论中所述,您要确保 fetchPlaces
是 returning 一个 Iterable
以便使用 _places.addAll(pvalue)
否则,如果它是单个对象,使用 _places.add(pvaule)
.