英文:
How to map and get data from List<Object> in Flutter
问题
以下是您要的翻译部分:
我正在使用CarouselSlider包,我使用的项目来自硬编码的数据。我想从List<Object>
中映射数据。这是我的对象:
final List<Object> imgListObject = [
{"image": "/imageAsset1", "text": 'Text 1'},
{"image": "/imageAsset2", "text": 'Text 2'},
{"image": "/imageAsset3", "text": 'Text 3'},
];
这是我打算获取数据的方式:
final List<Widget> imageSliders = imgListObject
.map(
(data) => Column(
children: [
Image.asset(data['image']),
Text(data['text']),
],
),
)
.toList();
我从data['image']
和data['text']
遇到了这个错误:
运算符'[]'未定义为'Object'类型。
我的完整代码:
class HowItWorksScreen extends StatelessWidget {
const HowItWorksScreen({super.key});
@override
Widget build(BuildContext context) {
ContentSession contentSession = Provider.of<ContentSession>(context, listen: false);
final CarouselController _carouselController = CarouselController();
final List<Object> imgListObject = [
{"image": AppGlobalVariables.howItWorksSliderImage1, "text": 'Text 1'},
{"image": AppGlobalVariables.howItWorksSliderImage2, "text": 'Text 2'},
{"image": AppGlobalVariables.howItWorksSliderImage3, "text": 'Text 3'},
];
final List<Widget> imageSliders = imgListObject
.map(
(data) => Column(
children: [
Image.asset(data['image']),
Text(data['text']),
],
),
)
.toList();
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50),
child: AppbarMain(),
),
body: Column(
children: [
CarouselSlider(
carouselController: _carouselController,
options: CarouselOptions(
viewportFraction: 0.8,
enableInfiniteScroll: true,
enlargeCenterPage: true,
autoPlay: true,
height: 400,
),
items: imageSliders,
),
SizedBox(
height: 40,
),
ElevatedButton(
onPressed: () async => Navigator.pushNamed(context, landingScreenRoute!),
child: Text(contentSession.stringsHomeScreen.signin),
),
],
),
);
}
}
希望这有助于您获得所需的输出。
英文:
I'm using CarouselSlider package and the items I used is from a hardcoded data. I'd like to get data from mapping it from a List<Object>
. Here is my object:
final List<Object> imgListObject = [
{"image": "/imageAsset1", "text": 'Text 1'},
{"image": "/imageAsset2", "text": 'Text 2'},
{"image": "/imageAsset3", "text": 'Text 3'},
];
And this is how I intend to get the data:
final List<Widget> imageSliders = imgListObject
.map(
(data) => Column(
children: [
Image.asset(data['image']),
Text(data['text']),
],
),
)
.toList();
I have this error from data['image']
and data['text']
:
The operator '[]' isn't defined for the type 'Object'.
My full code:
class HowItWorksScreen extends StatelessWidget {
const HowItWorksScreen({super.key});
@override
Widget build(BuildContext context) {
ContentSession contentSession = Provider.of<ContentSession>(context, listen: false);
final CarouselController _carouselController = CarouselController();
final List<Object> imgListObject = [
{"image": AppGlobalVariables.howItWorksSliderImage1, "text": 'Text 1'},
{"image": AppGlobalVariables.howItWorksSliderImage2, "text": 'Text 2'},
{"image": AppGlobalVariables.howItWorksSliderImage3, "text": 'Text 3'},
];
final List<Widget> imageSliders = imgListObject
.map(
(data) => Column(
children: [
Image.asset(data['image']),
Text(data['text']),
],
),
)
.toList();
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50),
child: AppbarMain(),
),
body: Column(
children: [
CarouselSlider(
carouselController: _carouselController,
options: CarouselOptions(
viewportFraction: 0.8,
enableInfiniteScroll: true,
enlargeCenterPage: true,
autoPlay: true,
height: 400,
),
items: imageSliders,
),
SizedBox(
height: 40,
),
ElevatedButton(
onPressed: () async => Navigator.pushNamed(context, landingScreenRoute!),
child: Text(contentSession.stringsHomeScreen.signin),
),
],
),
);
}
}
答案1
得分: 1
以下是您要翻译的内容:
问题很明显:
final List<Object> imgListObject = [
{"image": "/imageAsset1", "text": 'Text 1'},
{"image": "/imageAsset2", "text": 'Text 2'},
{"image": "/imageAsset3", "text": 'Text 3'},
];
这是一个包含Object
的List
,所以当您稍后调用:
Column(
children: [
Image.asset(data['image']),
Text(data['text']),
],
)
它无法访问data['image']
,因为它认为它是一个Object
而不是一个Map
。
您将需要显式地转换类型。您可以使用as
关键字来做到这一点:
final List<Widget> imageSliders = imgListObject.map(
(item) {
Map<String, String> data = item as Map<String, String>;
return Column(
children: [
// 使用`!`操作符来断言该值不为空
Image.asset(data['image']!),
Text(data['text']!),
],
);
},
).toList();
或者作为另一种选择,为imgListObject
指定正确的类型:
final List<Map<String, String>> imgListObject = [
{"image": "/imageAsset1", "text": 'Text 1'},
{"image": "/imageAsset2", "text": 'Text 2'},
{"image": "/imageAsset3", "text": 'Text 3'},
];
参考链接
-
https://stackoverflow.com/questions/55789048/what-does-the-as-keyword-do-in-dart-language
-
https://stackoverflow.com/questions/60245865/the-operator-isnt-defined-for-the-class-object-dart
英文:
The problem is obvious:
final List<Object> imgListObject = [
{"image": "/imageAsset1", "text": 'Text 1'},
{"image": "/imageAsset2", "text": 'Text 2'},
{"image": "/imageAsset3", "text": 'Text 3'},
];
is a List
of Object
's, so when you later call:
Column(
children: [
Image.asset(data['image']),
Text(data['text']),
],
it can't access data['image']
since it thinks it's an Object
not a Map
.
You'll have to explicitly cast the type. you can do so using the as
keyword:
final List<Widget> imageSliders = imgListObject.map(
(item) {
Map<String, String> data = item as Map<String, String>;
return Column(
children: [
// using the `!` operator to assert that the value is not null
Image.asset(data['image']!),
Text(data['text']!),
],
);
},
).toList();
Or as an alternative, specify the correct type for imgListObject
:
final List<Map<String, String>> imgListObject = [
{"image": "/imageAsset1", "text": 'Text 1'},
{"image": "/imageAsset2", "text": 'Text 2'},
{"image": "/imageAsset3", "text": 'Text 3'},
];
See also
答案2
得分: 1
以下是翻译好的代码部分:
const List<Object> imgListObject = [
{'image': '/imageAsset1', 'text': 'Text 1'},
{'image': '/imageAsset2', 'text': 'Text 2'},
{'image': '/imageAsset3', 'text': 'Text 3'},
];
List<Column> columns() => [
for (final item in imgListObject)
Column(
children: switch (item) {
{
'image': final String image,
'text': final String text,
} =>
[
Image.asset(image),
Text(text),
],
final v => throw Exception('what is $v'),
},
),
];
英文:
Here's another way to extract that, with patterns:
const List<Object> imgListObject = [
{'image': '/imageAsset1', 'text': 'Text 1'},
{'image': '/imageAsset2', 'text': 'Text 2'},
{'image': '/imageAsset3', 'text': 'Text 3'},
];
List<Column> columns() => [
for (final item in imgListObject)
Column(
children: switch (item) {
{
'image': final String image,
'text': final String text,
} =>
[
Image.asset(image),
Text(text),
],
final v => throw Exception('what is $v'),
},
),
];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论